-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
object.js
150 lines (130 loc) · 5.8 KB
/
object.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*!
* template-helpers <https://github.com/jonschlinkert/template-helpers>
*
* Copyright (c) 2015, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
var assert = require('assert');
var helpers = require('..')('object');
var template = require('lodash.template');
var context = {obj: {a: 'a', b: 'b', c: {d: {e: 'e'}}}};
var imports = {imports: helpers};
describe('objects', function() {
describe('fallback', function() {
it('should use the fallback value when the first value is undefined.', function() {
assert.equal(template('<%= fallback(a.b) %>', imports)({a: {b: 'b'}}), 'b');
assert.equal(template('<%= fallback(a.z, a.b) %>', imports)({a: {b: 'b'}}), 'b');
assert.equal(template('<%= fallback(x.k, x.z) %>', imports)({x: {z: 'z'}}), 'z');
});
});
describe('stringify', function() {
it('should stringify an object.', function() {
assert.equal(template('<%= stringify({a: "a"}) %>', imports)(context), '{"a":"a"}');
assert.equal(template('<%= stringify(obj.c) %>', imports)(context), '{"d":{"e":"e"}}');
});
});
describe('parse', function() {
it('should parse a string to an object:', function() {
assert.equal(template('<%= parse(\'{"foo":"bar"}\') %>', imports)(context), '[object Object]');
assert.equal(template('<%= parse(\'{"foo":"bar"}\')["foo"] %>', imports)(context), 'bar');
});
});
describe('isObject', function() {
it('should return true if the value is an object.', function() {
assert.equal(template('<%= isObject(obj) %>', imports)(context), 'true');
assert.equal(template('<%= isObject([]) %>', imports)(context), 'false');
assert.equal(template('<%= isObject("foo") %>', imports)(context), 'false');
});
});
describe('isPlainObject', function() {
it('should return true if the value is a plain object.', function() {
assert.equal(template('<%= isPlainObject(obj) %>', imports)(context), 'true');
assert.equal(template('<%= isPlainObject([]) %>', imports)(context), 'false');
assert.equal(template('<%= isPlainObject(/foo/) %>', imports)(context), 'false');
assert.equal(template('<%= isPlainObject("foo") %>', imports)(context), 'false');
});
});
describe('hasOwn', function() {
it('should return true when an object has own property `key`.', function() {
assert.equal(template('<%= hasOwn(obj, "a") %>', imports)(context), 'true');
assert.equal(template('<%= hasOwn(obj, "k") %>', imports)(context), 'false');
});
});
describe('keys', function() {
it('should return the keys of an object.', function() {
assert.equal(template('<%= keys(obj) %>', imports)(context), ['a', 'b', 'c'].toString());
});
});
describe('forIn', function() {
it('should expose the keys on an object.', function() {
var context = {values: {a: 'b', c: 'd'}}
var actual = template('<% forIn(values, function(val, key) { %><%= key %><% }) %>', imports)(context);
assert.equal(actual, 'ac');
});
it('should expose the values on an object.', function() {
var context = {values: {a: 'b', c: 'd'}}
var actual = template('<% forIn(values, function(val, key) { %><%= val %><% }) %>', imports)(context);
assert.equal(actual, 'bd');
});
});
describe('forOwn', function() {
it('should expose the keys on an object.', function() {
var context = { values: { a: 'b', c: 'd' } };
var actual = template('<% forOwn(values, function(val, key) { %><%= key %><% }) %>', imports)(context);
assert.equal(actual, 'ac');
});
it('should expose the values on an object.', function() {
var context = {values: {a: 'b', c: 'd'}}
var actual = template('<% forOwn(values, function(val, key) { %><%= val %><% }) %>', imports)(context);
assert.equal(actual, 'bd');
});
});
describe('omit', function() {
it('should omit keys from an object.', function() {
var actual = template('<%= stringify(omit(obj, ["b", "c"])) %>', imports)(context);
assert.equal(actual, '{"a":"a"}');
});
});
describe('extend', function() {
beforeEach(function() {
context.foo = {aaa: 'bbb'};
context.bar = {ccc: 'ddd'};
});
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= extend() %>', imports)(), '');
});
it('should ignore non-objects.', function() {
var actual = template('<%= stringify(extend(foo, bar, "baz")) %>', imports)(context);
assert.equal(actual, '{"aaa":"bbb","ccc":"ddd"}');
});
it('should extend the first object with the second.', function() {
var actual = template('<%= stringify(extend(foo, bar)) %>', imports)(context);
assert.equal(actual, '{"aaa":"bbb","ccc":"ddd"}');
});
it('should use the extended object as context.', function() {
// overwrite `foo`
context.bar = {aaa: 'ddd'};
var actual = template('<%= get(extend(foo, bar), "aaa") %>', imports)(context);
assert.equal(actual, 'ddd');
});
});
describe('merge', function() {
beforeEach(function() {
context.foo = {aaa: 'bbb', bbb: {ccc: {ddd: 'eee'}}};
context.bar = {aaa: 'bbb', bbb: {ccc: {eee: 'fff'}}};
context.baz = {aaa: 'bbb', bbb: {ccc: {fff: 'ggg'}}};
});
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= merge() %>', imports)(), '');
});
it('should merge objects.', function() {
var actual = template('<%= stringify(merge(foo, bar)) %>', imports)(context);
assert.equal(actual, '{"aaa":"bbb","bbb":{"ccc":{"ddd":"eee","eee":"fff"}}}');
});
it('should merge multiple objects:', function() {
var actual = template('<%= stringify(merge(foo, bar, baz)) %>', imports)(context);
assert.equal(actual, '{"aaa":"bbb","bbb":{"ccc":{"ddd":"eee","eee":"fff","fff":"ggg"}}}');
});
});
});