Skip to content

Commit d464bf5

Browse files
committed
feat: added interpolation
feat: added interpolation
1 parent 2d52f00 commit d464bf5

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/interpolation.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const format_string = (input, state) => {
2+
if (Object.entries(state).length === 0) return input
3+
const first_key = Object.keys(state)[0]
4+
const copy = Object.assign({}, state)
5+
delete copy[first_key]
6+
return format_string(input.replace('${' + first_key + '}', state[first_key]), copy)
7+
}
8+
9+
const format_array = (input, state) => {
10+
return input.map(item => format_value(item, state))
11+
}
12+
13+
const format_value = (input, state) => {
14+
if (input instanceof Array) return format_array(input, state)
15+
if (typeof input === "string") return format_string(input, state)
16+
if (typeof input === "object") return format_object(input, state)
17+
return input
18+
}
19+
20+
const format_object = (input, state) => {
21+
return Object.assign({}, ...Object.entries(input).map(
22+
([key, value]) => ({[format_string(key, state)]: format_value(value, state)})
23+
))
24+
}
25+
26+
export { format_string , format_array, format_value, format_object }

src/interpolation.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const module = require('./interpolation.js')
2+
3+
const string_tests = [
4+
['foo', 'foo', {}],
5+
['Hello World!', 'Hello World!', {}],
6+
['Hello ${name}', 'Hello World!', {'name': 'World!'}],
7+
['Hello ${name}!', 'Hello Person!', {'name': 'Person'}],
8+
['${greeting} ${name}!', 'Hola Person!', {'name': 'Person', 'greeting': 'Hola'}],
9+
['${foo} ${bar}!', 'Hola Person!', {'bar': 'Person', 'foo': 'Hola'}],
10+
]
11+
12+
const array_tests = [
13+
[[], [], {}],
14+
[['Hello ${name}!'], ['Hello World!'], {'name': 'World'}],
15+
[['Hello ${foo}!'], ['Hello World!'], {'foo': 'World'}],
16+
[['Hello ${foo}!', 'Hello ${foo}!'], ['Hello World!', 'Hello World!'], {'foo': 'World'}],
17+
[['Hello ${foo}!', ['Hello ${foo}!']], ['Hello World!', ['Hello World!']], {'foo': 'World'}],
18+
[['Hello ${foo}!', [{'${foo}': 42}]], ['Hello World!', [{'World': 42}]], {'foo': 'World'}],
19+
]
20+
21+
const static_tests = [
22+
[42, 42, {}],
23+
[false, false, {}],
24+
[889593.234234, 889593.234234, {}],
25+
]
26+
27+
const object_tests = [
28+
[{}, {}, {}],
29+
[{'foo': '${bar}'}, {'foo': 'hello'}, {'bar': 'hello'}],
30+
[{'foo': '${bar}', 'baz': '${asd}'}, {'foo': 'hello', 'baz': '33'}, {'bar': 'hello', 'asd': '33'}],
31+
[{'${bar}': '${bar}', 'baz': '${asd}'}, {'hello': 'hello', 'baz': '33'}, {'bar': 'hello', 'asd': '33'}],
32+
[{'${bar}': ['${bar}'], 'baz': '${asd}'}, {'hello': ['hello'], 'baz': '33'}, {'bar': 'hello', 'asd': '33'}],
33+
]
34+
35+
const tests = [
36+
['String interpolation', 'format_string', [string_tests]],
37+
['Array interpolation', 'format_array', [array_tests]],
38+
['Value interpolation', 'format_value', [string_tests, array_tests, static_tests, object_tests]],
39+
['Object interpolation', 'format_object', [object_tests]],
40+
]
41+
42+
tests.forEach(([test_suite_name, function_name, test_sets]) =>
43+
describe(test_suite_name, () => {
44+
test_sets.forEach(test_set =>
45+
test_set.forEach(([input, output, state]) =>
46+
it(`${[JSON.stringify(input), JSON.stringify(output), JSON.stringify(state)]}`, () => {
47+
module[function_name](input, state).should.deepEqual(output)
48+
})
49+
)
50+
)
51+
})
52+
)

0 commit comments

Comments
 (0)