Useful Object Extensions for Meteor
Date:
Date.now()- Fix for ES if Date object has not now() method
Math:
Math.getRandom(min, max)- Get random number betweenmaxandminvalues, by defaultmaxis1000andminis0
Object
Object.defineReactiveProperty(target, prop, value, callback, getCallback, setCallback)- define Reactive property with callbacks:- set callback before Setter and Getter
- set callback on Setter
- set callback on Getter
someArray.inArray(needle, searchInKey, searchByKey)- Extend Object prototype within inArray functionsomeArray.diff(comparableArray)- Compare two array, if matches found - return array of matches. If second parameter istruereturnstrue/falseif matches found/doesn't foundsomeObj.walk(callback)- Extend Object prototype within walk function callback,callback(object, value, key){}Object.Merge- Merge the enumerable attributes of two objects deeplysomeArray.random()- Extend Object prototype within random value pick up functionality for arrays
RegExp:
RegExp.escape(string)- Function is used to escape all dangerous/reserved symbols in strings before creating RegExp with it
String:
String.generate(length, symbols)- Generate random string from 1 to 10 symbols length, or with providedlength, and made of providedsymbolsString.random(Boolean)- Generate random string from 2 to 111 symbols length, if the only parameter istrue- generate string without unsafely chars. Additionally you can providelength, andsymbolsas first and second parameter, so this function will act as alias forString.generate(length, symbols)someString.clone()- Clone (a.k.a. create singleton) of String object. This method allows to resolve issue with variable's referencing. See performance here
clone() example:
1var str = 'abc'; 2var str2 = str.clone(); 3str = 'cde'; 4console.log(str2); //-> 'abc'