Giter Site home page Giter Site logo

amd-utils's People

Contributors

adamnowotny avatar conradz avatar millermedeiros avatar nowamasa avatar satazor avatar zship avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

amd-utils's Issues

versioning and NPM publish

Maybe we should do releases every month or so (or when we group a couple new features) and do point releases for bug fixes.

split specs into multiple files

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.

add function/batch()

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));

String#escapeHTML & String#unescapeHTML

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,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/'/g,"&#39;").replace(/"/g, "&quot;");
}

function unescapeHTML() {
    return this.replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&amp;/g,"&").replace(/&#39;/g,"'").replace(/&quot;/g, '"');
}

A possible implementation just for reference.

create string/normalizeLineBreaks()

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

move math/inRange and math/isNear to another package?

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...

automate "package modules" generation

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.

lang/toArray should convert NodeList

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.

move `lang/bind` to "function" package?

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.

number/toUInt and number/toUInt32 behavior

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:

  1. val <= 0? 0 : val >>> 0 (current);
  2. Math.abs(val) >>> 0 (same behavior as ES5 ToUint32 for what I could understand);
  3. val >>> 0 (same as AS3);

see this comment for more details.

Truncation returning incorrect results

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...

create array.join that ignores null/undefined items.

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..

array/indexOf should limit fromIndex and allow negative index

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

Add amd-utils to npm

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.

simplify ES5 array methods checks

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...

add object/clone()

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

refactor string/replaceAccents()

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)..

external documentation

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...

find a better name for time/split()

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.

lang/toArray() and falsy values

not sure how toArray() should handle falsy values ('', 0, false, null, undefined)... right now it is returning an empty array for these values.

add string/batchReplace()

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

convert modules to node.js?

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.

avoid calling Array.prototype natives?

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).

add string/repeat()

also remind to update number/pad to use it (basically extract the pad() logic..)

Closure compiler complains about reserved word char

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.