-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
array.ts
173 lines (172 loc) · 6.53 KB
/
array.ts
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { test } from '../../../stub/render'
import Liquid from '../../../../src/liquid'
import { expect } from 'chai'
describe('filters/array', function () {
let liquid: Liquid
beforeEach(function () {
liquid = new Liquid()
})
describe('join', function () {
it('should support join', function () {
const src = '{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}' +
'{{ beatles | join: " and " }}'
return test(src, 'John and Paul and George and Ringo')
})
it('should default separator to space', function () {
const src = '{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}' +
'{{ beatles | join }}'
return test(src, 'John Paul George Ringo')
})
})
it('should support split/last', function () {
const src = '{% assign my_array = "zebra, octopus, giraffe, tiger" | split: ", " %}' +
'{{ my_array|last }}'
return test(src, 'tiger')
})
it('should support map', function () {
return test('{{posts | map: "category"}}', 'foo,bar')
})
describe('reverse', function () {
it('should support reverse', async function () {
const html = await liquid.parseAndRender('{{ "Ground control to Major Tom." | split: "" | reverse | join: "" }}')
expect(html).to.equal('.moT rojaM ot lortnoc dnuorG')
})
it('should be pure', async function () {
const scope = { arr: ['a', 'b', 'c'] }
await liquid.parseAndRender('{{ arr | reverse | join: "" }}', scope)
const html = await liquid.parseAndRender('{{ arr | join: "" }}', scope)
expect(html).to.equal('abc')
})
})
describe('size', function () {
it('should return string length', async () => {
const html = await liquid.parseAndRender('{{ "Ground control to Major Tom." | size }}')
expect(html).to.equal('28')
})
it('should return array size', async () => {
const html = await liquid.parseAndRender(
'{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}{{ my_array | size }}')
expect(html).to.equal('4')
})
it('should be respected with <string>.size notation', async () => {
const html = await liquid.parseAndRender('{% assign my_string = "Ground control to Major Tom." %}{{ my_string.size }}')
expect(html).to.equal('28')
})
it('should be respected with <array>.size notation', async () => {
const html = await liquid.parseAndRender('{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}{{ my_array.size }}')
expect(html).to.equal('4')
})
it('should return 0 for false', async () => {
const html = await liquid.parseAndRender('{{ false | size }}')
expect(html).to.equal('0')
})
it('should return 0 for nil', async () => {
const html = await liquid.parseAndRender('{{ nil | size }}')
expect(html).to.equal('0')
})
it('should return 0 for undefined', async () => {
const html = await liquid.parseAndRender('{{ foo | size }}')
expect(html).to.equal('0')
})
})
describe('first', function () {
it('should support first', async () => {
const html = await liquid.parseAndRender(
'{{arr | first}}',
{ arr: [ 'zebra', 'tiger' ] }
)
expect(html).to.equal('zebra')
})
it('should return empty for nil', async () => {
const html = await liquid.parseAndRender('{{nil | first}}')
expect(html).to.equal('')
})
it('should return empty for undefined', async () => {
const html = await liquid.parseAndRender('{{foo | first}}')
expect(html).to.equal('')
})
it('should return empty for false', async () => {
const html = await liquid.parseAndRender('{{false | first}}')
expect(html).to.equal('')
})
it('should return empty for string', async () => {
const html = await liquid.parseAndRender('{{"zebra" | first}}')
expect(html).to.equal('')
})
})
describe('last', function () {
it('should support last', async () => {
const html = await liquid.parseAndRender(
'{{arr | last}}',
{ arr: [ 'zebra', 'tiger' ] }
)
expect(html).to.equal('tiger')
})
it('should return empty for nil', async () => {
const html = await liquid.parseAndRender('{{nil | last}}')
expect(html).to.equal('')
})
it('should return empty for undefined', async () => {
const html = await liquid.parseAndRender('{{foo | last}}')
expect(html).to.equal('')
})
it('should return empty for false', async () => {
const html = await liquid.parseAndRender('{{false | last}}')
expect(html).to.equal('')
})
it('should return empty for string', async () => {
const html = await liquid.parseAndRender('{{"zebra" | last}}')
expect(html).to.equal('')
})
})
describe('slice', function () {
it('should slice first char by 0', () => test('{{ "Liquid" | slice: 0 }}', 'L'))
it('should slice third char by 2', () => test('{{ "Liquid" | slice: 2 }}', 'q'))
it('should slice substr by 2,5', () => test('{{ "Liquid" | slice: 2, 5 }}', 'quid'))
it('should slice substr by -3,2', () => test('{{ "Liquid" | slice: -3, 2 }}', 'ui'))
it('should slice substr by -2,2', () => test('{{ "abc" | slice: -2, 2 }}', 'bc'))
it('should support array', () => test('{{ "1,2,3,4" | split: "," | slice: 1,2 | join }}', '2 3'))
})
it('should support sort', function () {
return test('{% assign my_array = "zebra, octopus, giraffe, Sally Snake"' +
' | split: ", " %}' +
'{{ my_array | sort | join: ", " }}',
'Sally Snake, giraffe, octopus, zebra')
})
describe('uniq', function () {
it('should uniq string list', function () {
return test(
'{% assign my_array = "ants, bugs, bees, bugs, ants" | split: ", " %}' +
'{{ my_array | uniq | join: ", " }}',
'ants, bugs, bees'
)
})
it('should uniq falsy value', function () {
return test('{{"" | uniq | join: ","}}', '')
})
})
describe('where', function () {
it('should support filter by property value', function () {
return test(`{% assign kitchen_products = products | where: "type", "kitchen" %}
Kitchen products:
{% for product in kitchen_products -%}
- {{ product.title }}
{% endfor %}`, `
Kitchen products:
- Spatula
- Garlic press
`)
})
it('should support filter truthy property', function () {
return test(`{% assign available_products = products | where: "available" %}
Available products:
{% for product in available_products -%}
- {{ product.title }}
{% endfor %}`, `
Available products:
- Coffee mug
- Boring sneakers
`)
})
})
})