-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
collection.js
63 lines (58 loc) · 2.89 KB
/
collection.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
/*!
* 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('..')(['collection', 'object']);
var template = require('lodash.template');
var imports = {imports: helpers};
describe('collections', function() {
describe('any', function() {
it('should be false undefined.', function() {
assert.equal(template('<%= any() %>', imports)(), 'false');
});
it('should return if a value exists in the given string.', function() {
assert.equal(template('<%= any("a-b-c", "a") %>', imports)(context), 'true');
assert.equal(template('<%= any("a-b-c", "d") %>', imports)(context), 'false');
});
it('should return if a value exists in the given object.', function() {
assert.equal(template('<%= any({a: "b", c: "d"}, "a") %>', imports)(context), 'true');
assert.equal(template('<%= any([{a: "b", c: "d"}], {a: "b"}) %>', imports)(context), 'true');
});
it('should return if a value exists in the given array.', function() {
assert.equal(template('<%= any("a-b-c", "d") %>', imports)(context), 'false');
});
});
describe('filter', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= filter() %>', imports)(), '');
});
it('should return a string value if it exists in the given array', function() {
assert.equal(template('<%= filter(["a", "b", "c"], "a") %>', imports)(context), 'a');
});
it('should return an empty string if a string value does not exists', function() {
assert.equal(template('<%= filter(["a", "b", "c"], "d") %>', imports)(context), '');
});
it('should filter out values that match the given regex', function() {
assert.equal(template('<%= filter(["a", "b", "c", "d", "z"], /[a-c]/) %>', imports)(context), 'a,b,c');
});
it('should filter out values using a function', function() {
assert.equal(template('<%= filter(["a", "b", "c", "d", "z"], function(key) { return /(a|z)/.test(key) }) %>', imports)(context), 'a,z');
});
it('should return true if the value equals given string', function() {
assert.equal(template('<%= filter("a", "a") %>', imports)(context), 'true');
});
it('should return false if the value does not equal the given string', function() {
assert.equal(template('<%= filter("a-b-c", "d") %>', imports)(context), 'false');
});
it('should filter out properties that match the given value', function() {
assert.equal(template('<%= stringify(filter({a: "b", c: "d"}, "a")) %>', imports)(context), '{"a":"b"}');
});
it('should filter out objects that match the given value', function() {
assert.equal(template('<%= stringify(filter([{a: "b", c: "d"}], {a: "b"})) %>', imports)(context), '[{"a":"b","c":"d"}]');
});
});
});