1 | const assert = require('assert');
|
2 | let object = require('../namespace/object');
|
3 | let {
|
4 | hasPath, set, get,
|
5 | extend, clone,
|
6 | arrayToObject, invert, createShallowInvertedClone,
|
7 | select, drop, selectCombination, format, explode
|
8 | } = object;
|
9 |
|
10 | describe("Object Methods", function () {
|
11 |
|
12 | it("atPath:: Should be able to pick the value of an object", function () {
|
13 | let o = {foo: {bar : 'foobar' }};
|
14 | assert.equal(get(o, 'foo.bar'), 'foobar');
|
15 | });
|
16 |
|
17 | it("atPath:: Should not blow up when the path requires traversal of undefined", function () {
|
18 | let o = {foo: {bar : 'foobar' }};
|
19 | assert.equal(get('foo.bar.baz.boze.ok', o), undefined);
|
20 | });
|
21 |
|
22 | it("extend:: should extend the src object", function () {
|
23 | let src = {one: 1};
|
24 | let ext = {two: 2};
|
25 | assert.deepEqual(extend(src, ext), {one: 1, two: 2});
|
26 | });
|
27 |
|
28 | it("extend:: should extend the src object with many objects", function () {
|
29 | let src = {one: 1};
|
30 | let ext = {two: 2};
|
31 | let ext2 = {three: 3};
|
32 | assert.deepEqual(extend(src, ext, ext2), {one: 1, two: 2, three: 3});
|
33 | });
|
34 |
|
35 | it("extend:: should overwrite src when keys are same", function () {
|
36 | let src = {one: 1};
|
37 | let ext = {one: 2};
|
38 | let ext2 = {three: 3};
|
39 | assert.deepEqual(extend(src, ext, ext2), {one: 2, three: 3});
|
40 | });
|
41 |
|
42 | it("extend:: should overwrite keys in order of args when keys are same", function () {
|
43 | let src = {one: 1};
|
44 | let ext = {two: 2};
|
45 | let ext2 = {two: 3};
|
46 | let ext3 = {two: 4};
|
47 | assert.deepEqual(extend(src, ext, ext2, ext3), {one: 1, two: 4});
|
48 | });
|
49 |
|
50 | it("hasPath:: should return a boolean value if an object contains a path", function () {
|
51 | let src = {one: 1};
|
52 | assert.equal(hasPath(src, 'one'), true);
|
53 | assert.equal(hasPath(src, 'one.value.at.nowhere'), false);
|
54 | });
|
55 |
|
56 | it("clone:: should return a new object", function () {
|
57 | let src = {one: 1, two: {a: 1}};
|
58 | assert.deepEqual(clone(src), {one: 1, two: {a: 1}});
|
59 | });
|
60 |
|
61 | it("clone:: new object should not be passed by reference", function () {
|
62 | let src = {one: 1, two: {a: 1} };
|
63 | let src2 = clone(src);
|
64 | src2.two.a = 3;
|
65 | assert.equal(src.two.a, 1);
|
66 | assert.equal(src2.two.a, 3);
|
67 | });
|
68 |
|
69 | it("set:: should set a value at the given path", function () {
|
70 | let src = {};
|
71 | set(src, 'foo', 1);
|
72 | assert.equal(src.foo, 1);
|
73 | });
|
74 |
|
75 | it("set:: should create the path if it does not exist, and set the value", function () {
|
76 | let src = {};
|
77 | set(src, 'foo.bar.baz.x', 1);
|
78 | assert.equal(src.foo.bar.baz.x, 1);
|
79 | });
|
80 |
|
81 | it("arrayToObject:: should convert an array of arrays to an object", function () {
|
82 | let arr = [['a', 1], ['b', 2]];
|
83 | let o = arrayToObject(arr);
|
84 | assert.equal(o.a, 1);
|
85 | assert.equal(o.b, 2);
|
86 | });
|
87 |
|
88 | it("invert:: should invert an object", function () {
|
89 | let o = {a: 'A', b: 'B'};
|
90 | invert(o);
|
91 | assert.deepEqual(o, {A: 'a', B: 'b'});
|
92 | assert.equal(o.a, undefined);
|
93 | });
|
94 |
|
95 | it("createShallowInvertedClone:: should create a shallow inverted clone", function () {
|
96 | let o = {a: 'A', b: 'B'};
|
97 | let x = createShallowInvertedClone(o);
|
98 | assert.deepEqual(x, {A: 'a', B: 'b'});
|
99 | assert.equal(x.a, undefined);
|
100 | assert.deepEqual(o, {a: 'A', b: 'B'});
|
101 | });
|
102 |
|
103 | it("select:: should select keys from an object", function () {
|
104 | let o = {name: 'fred', email: 'me@mail.com', password: 'secretSquirrel'};
|
105 | let fields = select(o, 'name', 'email');
|
106 | assert.deepEqual(fields, {name: 'fred', email: 'me@mail.com'});
|
107 | });
|
108 |
|
109 | it("select:: should select keys from an object via Array", function () {
|
110 | let o = {name: 'fred', email: 'me@mail.com', password: 'secretSquirrel'};
|
111 | let fields = select(o, ['name', 'email']);
|
112 | assert.deepEqual(fields, {name: 'fred', email: 'me@mail.com'});
|
113 | });
|
114 |
|
115 | it("drop:: should drop keys from an object", function () {
|
116 | let o = { name: 'fred', email: 'me@mail.com', password: 'secretSquirrel' };
|
117 | drop(o, 'password');
|
118 | assert.deepEqual(o, {name: 'fred', email: 'me@mail.com'});
|
119 | });
|
120 |
|
121 | it("drop:: should drop keys from an object via Array", function () {
|
122 | let o = { name: 'fred', email: 'me@mail.com', password: 'secretSquirrel' };
|
123 | drop(o, ['password']);
|
124 | assert.deepEqual(o, {name: 'fred', email: 'me@mail.com'});
|
125 | });
|
126 |
|
127 | it("selectCombination:: should select values from multiple objects and create a new one", function () {
|
128 | let a = { name: 'fred', email: 'me@mail.com', password: 'secretSquirrel' };
|
129 | let b = { sid: '8372487234', last_visit: new Date()};
|
130 | let c = { likes : 'stuff', knows: 'things'};
|
131 | let o = selectCombination([a, b, c], 'name', 'email', 'last_visit', 'likes', 'knows');
|
132 | assert.deepEqual(o, { name: 'fred', email: 'me@mail.com', last_visit: b.last_visit, likes : 'stuff', knows: 'things'});
|
133 | });
|
134 |
|
135 | it("format:: should format values properly (1)", function () {
|
136 | let a = { name: 'fred', email: 'me@mail.com', data: { stuff: { a: 'a', b: 'b'}, more : { c: 'value'} } };
|
137 | let o = format(a, 'name', 'email', ['stuff', 'data.stuff.a' ], ['value', 'data.more.c']);
|
138 | assert.deepEqual(o, { name: 'fred', email: 'me@mail.com', stuff: 'a', value : 'value'});
|
139 | });
|
140 |
|
141 | it("format:: should format values properly (2 - array)", function () {
|
142 | let a = { name: 'fred', email: 'me@mail.com', data: { stuff: { a: 'a', b: 'b'}, more : { c: 'value'} } };
|
143 | let o = format(a, ['name', 'email', ['stuff', 'data.stuff.a' ], ['value', 'data.more.c']]);
|
144 | assert.deepEqual(o, { name: 'fred', email: 'me@mail.com', stuff: 'a', value : 'value'});
|
145 | });
|
146 |
|
147 | it("format:: should format values properly (3)", function () {
|
148 | let a = { name: 'fred', email: 'me@mail.com', data: { stuff: { a: 'a', b: 'b'}, more : { c: 'value'} } };
|
149 | let o = format(a, 'name', 'email', 'data.stuff.a', ['value', 'data.more.c']);
|
150 | assert.deepEqual(o, { name: 'fred', email: 'me@mail.com', data: { stuff: { a: 'a'}}, value : 'value'});
|
151 | });
|
152 |
|
153 | it("explode:: should explode an object", function () {
|
154 | var row = {
|
155 | 'id': 2,
|
156 | 'contact.name.first': 'John',
|
157 | 'contact.name.last': 'Doe',
|
158 | 'contact.email': 'example@gmail.com',
|
159 | 'contact.info.about.me': 'classified',
|
160 | 'devices.0': 'mobile',
|
161 | 'devices.1': 'laptop',
|
162 | 'some.other.things.0': 'this',
|
163 | 'some.other.things.1': 'that',
|
164 | 'some.other.stuff.0.key': 'stuff'
|
165 | };
|
166 |
|
167 | object.explode(row);
|
168 |
|
169 | assert.deepEqual(row, {
|
170 | "id": 2,
|
171 | "contact": {
|
172 | "name": {
|
173 | "first": "John",
|
174 | "last": "Doe"
|
175 | },
|
176 | "email": "example@gmail.com",
|
177 | "info": {
|
178 | "about": {
|
179 | "me": "classified"
|
180 | }
|
181 | }
|
182 | },
|
183 | "devices": [
|
184 | "mobile",
|
185 | "laptop"
|
186 | ],
|
187 | "some": {
|
188 | "other": {
|
189 | "things": [
|
190 | "this",
|
191 | "that"
|
192 | ],
|
193 | stuff : [{
|
194 | key : "stuff"
|
195 | }]
|
196 | }
|
197 | }
|
198 | });
|
199 | });
|
200 |
|
201 | });
|