Skip to content

Commit 93bccfd

Browse files
committed
✒ Add parsing functionality migrated from deprecated repository.
1 parent 3eb04ab commit 93bccfd

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

source/parse.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// 📦 Parse string to object by different options.
2+
3+
// 'x=y' --> { x: y }
4+
export function parseKeyValuePairSeparatedBySymbolConcatenatedString({ string, delimiter = ' ', separatingSymbol = '=' }) {
5+
return parseKeyValuePairSeparatedBySymbolFromArray({
6+
array: string.split(delimiter),
7+
separatingSymbol
8+
})
9+
}
10+
11+
// ['x=y'] --> { x: y }
12+
export function parseKeyValuePairSeparatedBySymbolFromArray({ array, separatingSymbol = '=' }) {
13+
let parsedObject = {}
14+
array.forEach( pair => {
15+
let array = pair.split(separatingSymbol);
16+
array[1] && (parsedObject[array[0]] = array[1]);
17+
})
18+
return parsedObject
19+
}
20+
21+
export function combineKeyValueObjectIntoString({ object, delimiter = ' ', separatingSymbol='=' }) {
22+
return Object.keys(object)
23+
.reduce((previous, key) => {
24+
previous.push(`${key}${separatingSymbol}${object[key]}`)
25+
return previous;
26+
}, [])
27+
.join(delimiter)
28+
}

source/script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './behavior.js'
55
export * from './check.js'
66
export * from './convert.js'
77
export * from './get.js'
8+
export * from './parse.js'
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// import assert from 'assert'
2+
import path from 'path'
3+
import { assert } from 'chai'
4+
import configuration from '../setup/configuration'
5+
import { parseKeyValuePairSeparatedBySymbolConcatenatedString, parseKeyValuePairSeparatedBySymbolFromArray, combineKeyValueObjectIntoString } from '../source/parse.js'
6+
const assetFolder = path.join(configuration.directory.application.containerAbsolutePath, 'test/asset')
7+
8+
describe('function parseKeyValuePairSeparatedBySymbolConcatenatedString: ', function() {
9+
describe('string input containing key-value pairs', function() {
10+
it('Should parse/detect key value pairs', function() {
11+
let parsed = parseKeyValuePairSeparatedBySymbolConcatenatedString({ string: 'x=1 y=2 z t=3' }),
12+
expected = { x: '1', y: '2', t: '3' }
13+
assert.deepEqual(parsed, expected)
14+
})
15+
})
16+
})
17+
18+
describe('function parseKeyValuePairSeparatedBySymbolFromArray: ', function() {
19+
describe('array input containing key-value pairs', function() {
20+
it('Should parse/detect key value pairs separated by a specific symbol', function() {
21+
let parsed = parseKeyValuePairSeparatedBySymbolFromArray({ array: [ 'x_1', 'y_2', 't_3' ], separatingSymbol: '_' }),
22+
expected = { x: '1', y: '2', t: '3' }
23+
assert.deepEqual(parsed, expected)
24+
})
25+
})
26+
})
27+
28+
describe('function combineKeyValueObjectIntoString: ', function() {
29+
describe('Convert object to string with key-value separated structure', function() {
30+
it('Should encode object into string', function() {
31+
let actual = combineKeyValueObjectIntoString({ object: { x: '1', y: '2', t: '3' }, separatingSymbol: '_' }),
32+
expected = 'x_1 y_2 t_3'
33+
assert.equal(actual, expected)
34+
})
35+
})
36+
})
37+
38+

0 commit comments

Comments
 (0)