Universe Utilities
Installation
$ meteor add universe:utilities
Functions
UniUtils.set(object, pathInObject, value)
Creates an empty object inside namespace if not existent.
UniUtils.set({}, 'a.b.c', 'here'); //output: Object {a:{b:{c:"here"}}}
UniUtils.get(object, pathInObject, defaultValue)
Returns nested key value.
@param obj @param key @example var obj = { foo : { bar : 11 } }; get(obj, 'foo.bar'); // "11" get(obj, 'ipsum.dolorem.sit'); // undefined @returns {*} found key or undefined if key doesn't exist.
UniUtils.has
Checks if object contains a child key. Useful for cases where you need to check if an object contain a nested key.
UniUtils.findKey
Search key in object or array
@param obj or array @param search predicate function or value @param context
UniUtils.deepExtend
Recursive object extending.
1var obj1 = { 2 a: 1, 3 b: 2, 4 d: { 5 a: 1, 6 b: [], 7 c: { test1: 123, test2: 321 } 8 }, 9 f: 5, 10 g: 123, 11 i: 321, 12 j: [1, 2] 13}; 14var obj2 = { 15 b: 3, 16 c: 5, 17 d: { 18 b: { first: 'one', second: 'two' }, 19 c: { test2: 222 } 20 }, 21 e: { one: 1, two: 2 }, 22 f: [], 23 g: (void 0), 24 h: /abc/g, 25 i: null, 26 j: [3, 4] 27}; 28 29UniUtils.deepExtend(obj1, obj2); 30 31console.log(obj1); 32/* 33{ a: 1, 34 b: 3, 35 d: 36 { a: 1, 37 b: { first: 'one', second: 'two' }, 38 c: { test1: 123, test2: 222 } }, 39 f: null, 40 g: undefined, 41 c: 5, 42 e: { one: 1, two: 2 }, 43 h: /abc/g, 44 i: null, 45 j: [3, 4] } 46*/
- based on unclechu/node-deep-extend
UniUtils.deepEqual
Node's assert.deepEqual() algorithm as a standalone module. This module is around 5 times faster than wrapping assert.deepEqual() in a try/catch.
1 2console.dir([ 3 UniUtils.deepEqual( 4 { a : [ 2, 3 ], b : [ 4 ] }, 5 { a : [ 2, 3 ], b : [ 4 ] } 6 ), 7 UniUtils.deepEqual( 8 { x : 5, y : [6] }, 9 { x : 5, y : 6 } 10 ) 11]);
UniUtils.deepEqual(a, b, opts) Compare objects a and b, returning whether they are equal according to a recursive equality algorithm.
If opts.strict is true, use strict equality (===) to compare leaf nodes. The default is to use coercive equality (==) because that's how assert.deepEqual() works by default.
- based on substack/node-deep-equal
UniUtils.assign
Ponyfill: A polyfill that doesn't overwrite the native method and use native if available
1UniUtils.assign({foo: 0}, {bar: 1}); 2//=> {foo: 0, bar: 1} 3 4// multiple sources 5UniUtils.assign({foo: 0}, {bar: 1}, {baz: 2}); 6//=> {foo: 0, bar: 1, baz: 2} 7 8// overwrites equal keys 9UniUtils.assign({foo: 0}, {foo: 1}, {foo: 2}); 10//=> {foo: 2} 11 12// ignores null and undefined sources 13UniUtils.assign({foo: 0}, null, {bar: 1}, undefined); 14//=> {foo: 0, bar: 1}
UniUtils.assign(target, source, [source, ...])
Assigns enumerable own properties of source objects to the target object and returns the target object. Additional source objects will overwrite previous ones.
- more here: ES6 spec - Object.assign
- based on sindresorhus/object-assign
UniUtils.getFieldsFromUpdateModifier(modifier)
Gets array of top-level fields, which will be changed by modifier (this from update method)
UniUtils.getFieldsFromUpdateModifier({$set: {a:1, b:2, c:4}, $inc: {d:1}}); // output: ["a", "b", "c", "d"]
UniUtils.getPreviewOfDocumentAfterUpdate(updateModifier, oldDoc = {})
Gets simulation of new version of document passed as a second argument
UniUtils.getPreviewOfDocumentAfterUpdate({$set: {a:1, b:2, c:4}, $inc: {d:1}}, {a:2}); // output: Object {a: 1, b: 2, c: 4, d: 1}
UniConfig
- provides a simple configuration mechanism.
UniConfig provides on client side reactive method ready() (it's available on server too but always returns true)
and hook onReady()
, which calls passed callback only when config is ready.
UniConfig.onReady(function(){ if(this.public.get('myKey')){ //do something } });
All types have methods get, set, getRow. But arguments for individual types can be different.
UniConfig.private - this type of config is available ONLY on server side.
.get (name, defaultValue) .set (name, value) .getRow (name) .runOnce(name, function)
UniConfig.private.runOnce - call function only once and save date about this in private config, but if function threw error or returned false. function will be not check as executed.
UniConfig.users - this one is dedicated for users, it's available on both sides but on client it contains only stuff for logged in user.
.get (name, defaultValue, userId) .set (name, value, userId) .getRow (name, userId) * userId is needed only on server side
UniConfig.public - this type is available on both sides, every can change setting, unless it was added with true value passed as the third parameter of set method.
.get (name, defaultValue) .set (name, value, isServerWriteOnly) .getRow (name)
- Writing Access in public scope
Package will grant the writing access in client side for:
- every single user if this package is used without universe:collection
package.
- but if universe:collection
is added to the project only admins can set records
You can validate access right for client calls by registering own validator:
UniConfig.public.onAccessValidation(function(userId, settingObject, proposedResult){ var user = Meteor.users.findOne(userId); return user && user.hasAccessForThis; })
##And many more - check the source##