-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
155 lines (112 loc) · 4.82 KB
/
test.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
151
152
153
154
155
var test = require('tape');
var format = require('./index')
test('it should format strings with different attributes of an object', function (t) {
let string = 'Hello {name}, happy {age} bday!'
let object = { name: 'Bob', age: 32 }
let result = format(string, object)
t.equals(result, 'Hello Bob, happy 32 bday!');
t.end();
});
test('it should format strings with nested attributes', function (t) {
let string = 'Hello {bob.name}, happy {bob.age} bday! I call you at {bob.contact.phone}'
let object = {
bob: {
name: 'Bob',
age: 32,
contact: {
phone: '978090909'
}
}
}
let result = format(string, object)
t.equals(result, 'Hello Bob, happy 32 bday! I call you at 978090909');
t.end();
});
test('it should format strings passing arrays', function (t) {
let string = 'Hello {0}, happy {1} bday!'
let array = ['Bob', 32]
let result = format(string, array)
t.equals(result, 'Hello Bob, happy 32 bday!');
t.end();
});
test('it should format strings passing arrays of objects', function (t) {
let string = '{0.name} is {0.age}, {1.name} is {1.age}, {2.name} is {2.age}'
let array = [{ name: 'Bob', age: 32 }, { name: 'Mary', age: 30 }, { name: 'Baby', age: 0 }]
let result = format(string, array)
t.equals(result, 'Bob is 32, Mary is 30, Baby is 0');
t.end();
});
test('it should format strings spreading arrays of objects, using $n', function (t) {
let string = 'Hello {$n.name}!'
let array = [{ name: 'bob' }, { name: 'mary' }, { name: 'julia' }]
let result = format(string, array)
t.equals(result, 'Hello bob,mary,julia!');
t.end();
});
test('it should format strings spreading object:array:object, using $n', function (t) {
let string = 'Hello {people.$n.name}!'
let array = { people: [{ name: 'bob' }, { name: 'mary' }, { name: 'julia' }] }
let result = format(string, array)
t.equals(result, 'Hello bob,mary,julia!');
t.end();
});
test('it should format strings spreading object:array:object, using $n and numbers as value', function (t) {
let string = 'Ages: {people.$n.age}'
let array = { people: [{ name: 'bob', age: 22 }, { name: 'mary', age: 30 }, { name: 'baby', age: 0 }] }
let result = format(string, array)
t.equals(result, 'Ages: 22,30,0');
t.end();
});
test('it should format strings spreading object:array:object:array, using $n', function (t) {
let string = 'Hello {people.$n.names.$n}!'
let array = { people: [{ names: ['bob', 'abc', 'def'] }, { names: ['mary', 'ghi', 'jkl'] }] }
let result = format(string, array)
t.equals(result, 'Hello bob,abc,def,mary,ghi,jkl!');
t.end();
});
test('it should format strings spreading object:array:object:array:object, using $n', function (t) {
let string = 'Hello {people.$n.names.$n.name}!'
let array = { people: [
{ names: [{ name:'bob' }, { name:'abc' }, { name:'def' }] },
{ names: [{ name:'mary' }, { name:'ghi' }, { name:'jkl' }]}
]
}
let result = format(string, array)
t.equals(result, 'Hello bob,abc,def,mary,ghi,jkl!');
t.end();
});
test('it should format strings using a custom spreadToken', function (t) {
let string = 'Hello {people.$$.name}!'
let array = { people: [{ name: 'bob' }, { name: 'mary'}, { name: 'julia'}] }
let result = format(string, array, { spreadToken: '$$' })
t.equals(result, 'Hello bob,mary,julia!');
t.end();
});
test('it should format strings using a custom spreadSeparator', function (t) {
let string = 'Hello {people.$n.name}!'
let array = { people: [{ name: 'bob' }, { name: 'mary'}, { name: 'julia'}] }
let result = format(string, array, { spreadSeparator: ' ' })
t.equals(result, 'Hello bob mary julia!');
t.end();
});
test('it should *not* skip undefined attributes when *not* using the option skipUndefined', function (t) {
let string = 'Hello {name}, happy {age} bday! I call you at {bob.contact.phone}'
let object = { name: 'Bob' }
let result = format(string, object)
t.equals(result, 'Hello Bob, happy bday! I call you at ');
t.end();
});
test('it should skip undefined attributes when using the option skipUndefined', function (t) {
let string = 'Hello {name}, happy {age} bday! I call you at {bob.contact.phone}'
let object = { name: 'Bob' }
let result = format(string, object, { skipUndefined: true })
t.equals(result, 'Hello Bob, happy {age} bday! I call you at {bob.contact.phone}');
t.end();
});
test('it should format strings with a different format syntax', function (t) {
let string = 'Hello {{name}}, happy {{age}} bday!'
let object = { name: 'Bob', age: 32 }
let result = format(string, object, { regex: /{{(.*?)}}/g })
t.equals(result, 'Hello Bob, happy 32 bday!');
t.end();
});