millermedeiros / amd-utils Goto Github PK
View Code? Open in Web Editor NEWmodular JavaScript utilities written in the AMD format
Home Page: http://millermedeiros.github.com/amd-utils
modular JavaScript utilities written in the AMD format
Home Page: http://millermedeiros.github.com/amd-utils
since it only iterates over own properties...
Maybe we should do releases every month or so (or when we group a couple new features) and do point releases for bug fixes.
copy RegExp from crossroads patternLexer: https://github.com/millermedeiros/crossroads.js/blob/master/dist/crossroads.js#L343
modules are getting too big, it's better to split specs by each function so it's easier to update and can also be run separately.
allow a custom callback to set the value to compare, similar to underscore#max
since there are many random related methods and they don't really fit inside math.
this is dumb, poor unit tests... please fix it :P
a function that executes multiple functions at once so you can combine multiple forEach
into a single one.
forEach(myArr, batch(console.log, foo, bar, ipsum));
It would be nice to have these two functions because sometimes we do not want to strip tags but escape them instead.
function escapeHTML() {
return this.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/'/g,"'").replace(/"/g, """);
}
function unescapeHTML() {
return this.replace(/</g,"<").replace(/>/g,">").replace(/&/g,"&").replace(/'/g,"'").replace(/"/g, '"');
}
A possible implementation just for reference.
remove it since it isn't reliable and not really useful (can check if it is a array-like object..)
I always need to convert line breaks from DOS/MAC to UNIX when doing some file parsing... basically:
str
.replace(/\r\n/g,"\n") // DOS to Unix
.replace(/\r/g,"\n"); // Mac to Unix
too many modules so it would be great if the build script could show warnings for modules that doesn't have specs or documentation... need to implement "concat" option on mdoc first.
automate ALL things!
since shuffle()
is inside the array package I fell that pick()
should also be.
I think it discourages people from contributing.
people can use the impact and contributors pages to check who did what, I don't won't be the owner of the code, just the first contributor.
since now there is a project page with documentation maybe it is a good idea to move the README files to another folder (and rename them) so user don't need to keep the files on their machine if they don't want to... specially since the documentation will probably get larger with time (more examples, more modules..)
Both methods aren't really related to math.. it's more a logic
/condition
thing. Considering to create a new package just don't know the name yet, have other methods that would fit the same category like a method that returns the first non-null argument...
It would be nice to have a function that receives a date object or a valid date string and converts it to a more human readable date:
Some libraries that implement this feature:
"Linear ratio" is less common than "normalize" so method should be called norm()
instead of lratio()
...
since I'm only using the individual modules I always forget to update the modules that loads all scripts from the packages... add it to the node build
script.
The current version of Object mixIn, 0.1.1 (2012/01/19), contains a line of dead code (may be useful for debugging, though):
https://github.com/millermedeiros/amd-utils/blob/master/src/object/mixIn.js
Dead code line:
n = arguments.length,
NodeList doesn't have all the Array methods and is a pseudo-array. Maybe change the logic to not match String
, RegExp
window
, document
and Function
but match all other objects that has the property length
.
in case value is a string or any other object that has a .length
property (string, regexp, function, etc)
before we didn't have the "function" package.. now it fells weird to require the bind
method from the lang
package even tho the bind
is used to solve scope issues related to the "language".
maybe create an alias like:
// function/bind.js
define(['../lang/bind'], function(bind){
return bind;
});
this can also be "solved" with the RequireJS map config, so no need for an extra file or changes in the lib:
require.config({
map : {
'*' : {
'amd-utils/function/bind' : 'amd-utils/lang/bind'
}
}
});
not a big deal and I'm not even sure if the correct place would be inside the function
package, both or just keep it inside lang
...
PS: I had the same question about lang/clone
since the original need was to deep clone objects, than I realized it was also useful/needed for Array/Date/RegExp, so I decided to just keep it inside lang
.
right now if val < 0
it will return 0
, I'm not sure if that is the right behavior.
In AS3 calling uint(-1.5)
will return 4294967295
.
According to the ES5 spec ToUint32(-1.5)
will return 1
(at least I think so.. not sure)
3 possible behaviors:
val <= 0? 0 : val >>> 0
(current);Math.abs(val) >>> 0
(same behavior as ES5 ToUint32 for what I could understand);val >>> 0
(same as AS3);When testing your methods standalone, the returned truncated string is incorrect.
function truncate(str, maxChars, append){
maxChars = maxChars || 125;
append = append || '...';
str = trim(str);
if(str.length <= maxChars){
return str;
}
str = str.substr(0, maxChars - append.length + 1);
str = str.substr(0, str.lastIndexOf(' ')); //crop at last space
return str + append;
}
function trim(str){
return (str || '').replace(/^\s+|\s+$/g, '');
}
truncate('Lorem ipsum dolor sit amet', 6); // ...
truncate('Lorem ipsum dolor sit amet', 7); // ...
truncate('Lorem ipsum dolor sit amet', 13); // Lorem...
lots of times I need to join multiple words but making sure they do exist like:
prop = prefix? prefix +' '+ prop : prop;
it would be way more elegant if it could be written like:
prop = arrayUtils.join([prefix, prop], ' ');
specially if we want to join more than 2 words..
current implementation isn't ES5 compliant.
indexOf
The index at which to begin the search. Defaults to 0, i.e. the whole array will be searched. If the index is greater than or equal to the length of the array, -1 is returned, i.e. the array will not be searched. If negative, it is taken as the offset from the end of the array. Note that even when the index is negative, the array is still searched from front to back. If the calculated index is less than 0, the whole array will be searched.
source: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
see python set
type documentation: union, intersection, difference.
check other python set
methods as well
remove the "wrong" javadocs, some of them are in a format that closure compiler can't understand.
At the moment we have mixIn that merges two objects but not recursively.
We could have something similar to mootools Object.merge
Adding amd-utils to the npm will allow projects to specify the depency to amd-utils in the package.json.
This has an advantage over traditional git submodules because npm can handle repetitive dependencies, that is, if dependency1 and dependency2 both depends on amd-utils, it won't be downloaded twice.
I followed the same structure as underscore.js for the checks, eg:
function forEach(arr, callback, thisObj){
if(_nativeForEach && arr.forEach === _nativeForEach){
arr.forEach(callback, thisObj);
} else {
...
}
}
I think it would be better written as:
var forEach = Array.prototype.forEach?
function (arr, fn, thisObj) {
arr.forEach(fn, thisObj);
} :
function (arr, fn, thisObj) {
...
};
it will be better for performance since check will only happen once and I'm not using those methods on anything besides Arrays (it's even inside the "array" package...). if user want to iterate over an object he can simply use the lang/toArray method - note that current implementation doesn't work on regular objects anyways so check is indeed dumb...
sometimes I want to test the methods while outside an application, it would be great if docs had some sort or REPL like javascript sandbox console
it should be a deep clone since a shallow one can be faked by using object/mixIn()
passing an empty object as first argument.
this feature is blocking issue #49
not sure what is the problem yet, error msg:
should handle primitives and other objects
TypeError: JScript object expected
see #20
right now I have a huge chain of .replace
calls, it would "compress better" (smaller file size) if replacements was done in a loop and we used a lookup table (maybe just an array with all the replacements)..
right now I have JavaDoc style comments on most of the methods (if not all of them) but never tried to run JSDoc-toolkit on it.. not even sure if nowadays it already supports the @exports
... maybe the best option will be to keep documentation as a separate file altogether, just some markdown files, it will be easier to add extra info about what the method does and how can it be useful...
split on a string returns an Array and since now I've been using way more the functions disconnected from the package modules the names should be descriptive.
considering parseTime()
, parseTimestamp()
, parseMs()
so far.
it seems the problem is related to the way array/some
works on non-standard browsers... the loop is supposed to be incremental.
not sure how toArray()
should handle falsy values (''
, 0
, false
, null
, undefined
)... right now it is returning an empty array for these values.
create a separate method that abstracts batch replacements since it can be useful for cases like HTML entities encode/decode...
so instead of coding:
str = str
.replace(/\s+/g, '-')
.replace(/foo/g, 'bar')
...
it could be written as:
str = batchReplace(str, [
[/\s+/g, '-'],
[/foo/g, 'bar']
]);
see #14
sometimes I see myself writing a node.js script and want to use some of the amd-utils methods and end up copying and pasting the method I need... it would be way easier if amd-utils worked as a npm package as well (out of the box).
need to check if someone already coded a tool to convert AMD code into CJS. if the AMD -> node.js
tool doesn't exist we could write a script that appends amdefine to the top of each module and output it into a separate folder (just for node), not the most elegant solution but would work.
That way it would be possible to call these methods over array-like objects (ie. jQuery collections) and we also wouldn't need to worry if behavior is exactly like the ES5 spec in every scenario* (will probably avoid headaches).
Another gain is that it would make the definition of some of the methods simpler (no need to check if native exists) and would avoid conflicts with other libs that modifies the natives as well.
I've seen some info about the native Array#forEach
and other similar methods being slower than a custom implementation using while/for
loops but that isn't the main reason for the change specially since I think perf will change in the future.
Affected methods:
PS: I won't drop support for sparse Arrays and will try to keep them as compatible as possible (for now keep the current implementation just avoid calling the natives).
choice(-1,1) // 50% chance -1, 50% chance of 1 -- the same as randInt(-1,1,true)
choice(2,3,5,7) // 25% chance each of 2, 3, 5, or 7
also remind to update number/pad
to use it (basically extract the pad()
logic..)
maybe even add an option to set the default.
Although it is not used in the language, char is a reserved word in Javascript. I wasn't even aware of it, but I searched and found this: http://www.crockford.com/javascript/survey.html (the very bottom of the page)
The code runs fine, but Google's closure compiler doesn't like it and spits out errors for both amd-utils/string/lpad and amd-utils/string/rpad. Here's the output:
Apr 27, 2012 7:27:47 PM com.google.javascript.jscomp.parsing.ParserRunner parse
INFO: Error parsing /Users/tcjr/.../utils/string/lpad.js: Compilation produced 7 syntax errors.
(/Users/tcjr/.../utils/string/lpad.js#1)
/Users/tcjr/.../utils/string/lpad.js:7: ERROR - Parse error. missing formal parameter
function lpad(str, minLen, char) {
^
/Users/tcjr/.../utils/string/lpad.js:7: ERROR - Parse error. missing } after function body
function lpad(str, minLen, char) {
^
/Users/tcjr/.../utils/string/lpad.js:8: ERROR - Parse error. syntax error
char = char || ' ';
^
/Users/tcjr/.../utils/string/lpad.js:9: ERROR - Parse error. invalid return
return (str.length < minLen)? repeat(char, minLen - str.length) + str : str;
^
/Users/tcjr/.../utils/string/lpad.js:9: ERROR - Parse error. identifier is a reserved word
return (str.length < minLen)? repeat(char, minLen - str.length) + str : str;
^
/Users/tcjr/.../utils/string/lpad.js:10: ERROR - Parse error. syntax error
}
/Users/tcjr/.../utils/string/lpad.js:14: ERROR - Parse error. syntax error
});
^
7 error(s), 0 warning(s)
(elipses are mine to shorten the paths)
Anyway, I changed char to ch locally and everything works fine. I don't think it merits a fork and pull request, but you may want to update it.
Great library!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.