universe:utilities

v2.3.2Published 8 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

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.

Recursive Iterator

Iterates javascript object recursively. Works in ES6 environments iteration protocols. Compatible with for...of cycle.

1var iterator = new UniUtils.RecursiveIterator(
2    root /*{Object|Array}*/,
3    // Method bypass of tree: `vertical` or `horizontal`
4    [bypassMode="vertical"] /*{String}*/, 
5    [ignoreCircular=false] /*{Boolean}*/,
6    [maxDeep=100] /*{Number}*/
7);
8
9var {value, done} = iterator.next();
10var {parent, node, key, path, deep} = value;
11
12// parent is parent node
13// node is current node
14// key is key of node
15// path is path to node
16// deep is current deep
  • iterator.isNode(node);

Returns true if any is node (as a default node is {Object}).

  • iterator.isLeaf(node);

Returns true if node is leaf. Leaf is all primitive types.

  • iterator.isCircular(node);

Returns true if object is circular reference.

  • iterator.onStepInto(state); // state = iterator.next();

It calls for each node. If returns false node will be skipped. You can override this function, if you need to have custom behavior

Example (es6)

1var root = {
2    object: {
3        number: 1
4    },
5    string: 'foo'
6};
7
8for(let {node, path} of new UniUtils.RecursiveIterator(root)) {
9    console.log(path.join('.'), node);
10}
11
12// object    Object {number: 1}
13// object.number    1
14// string    foo

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*/

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.

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#

Licence

This package is distributed under MIT

Some parts of this package was based under: