Skip to content

Commit 06b4077

Browse files
committed
feat: added toObject function
1 parent a1d9a97 commit 06b4077

File tree

2 files changed

+60
-19
lines changed

2 files changed

+60
-19
lines changed

src/properties.spec.ts

+43-19
Original file line numberDiff line numberDiff line change
@@ -227,29 +227,53 @@ describe('data access', () => {
227227
expect(config.lines).toEqual([])
228228
})
229229

230-
it('should list all key-value pairs', () => {
231-
const result = [...properties.list(sample)]
232-
const resultAsArrays = result.map(({key, value}) => [key, value])
230+
describe('list', () => {
231+
it('should list all key-value pairs', () => {
232+
const result = [...properties.list(sample)]
233+
const resultAsArrays = result.map(({key, value}) => [key, value])
233234

234-
expect(resultAsArrays).toEqual(samplePairs)
235+
expect(resultAsArrays).toEqual(samplePairs)
236+
})
235237
})
236238

237-
it('should return all keys toMap', () => {
238-
const result = properties.toMap(sample)
239-
expect([...result.entries()]).toEqual(samplePairs)
239+
describe('toObject', () => {
240+
it('should return all pairs', () => {
241+
const result = properties.toObject(sample)
242+
expect(Object.entries(result)).toEqual(samplePairs)
243+
})
244+
245+
it('should return last value of duplicate key', () => {
246+
const config: properties.Properties = {
247+
lines: ['foo=bar1', 'a=b', 'foo=bar2', 'foo=bar3', 'c=d']
248+
}
249+
250+
const result = properties.toObject(config)
251+
expect(Object.entries(result)).toEqual([
252+
['foo', 'bar3'],
253+
['a', 'b'],
254+
['c', 'd']
255+
])
256+
})
240257
})
241258

242-
it('should return last value of duplicate key', () => {
243-
const config: properties.Properties = {
244-
lines: ['foo=bar1', 'a=b', 'foo=bar2', 'foo=bar3', 'c=d']
245-
}
259+
describe('toMap', () => {
260+
it('should return all pairs', () => {
261+
const result = properties.toMap(sample)
262+
expect([...result.entries()]).toEqual(samplePairs)
263+
})
246264

247-
const result = properties.toMap(config)
248-
expect([...result.entries()]).toEqual([
249-
['foo', 'bar3'],
250-
['a', 'b'],
251-
['c', 'd']
252-
])
265+
it('should return last value of duplicate key', () => {
266+
const config: properties.Properties = {
267+
lines: ['foo=bar1', 'a=b', 'foo=bar2', 'foo=bar3', 'c=d']
268+
}
269+
270+
const result = properties.toMap(config)
271+
expect([...result.entries()]).toEqual([
272+
['foo', 'bar3'],
273+
['a', 'b'],
274+
['c', 'd']
275+
])
276+
})
253277
})
254278
})
255279

@@ -270,7 +294,7 @@ describe('The property key escaping', () => {
270294
['\\foo12\\', '\\\\foo12\\\\'],
271295
['\0\u0001', '\\u0000\\u0001'],
272296
['\u3053\u3093\u306B\u3061\u306F', '\\u3053\\u3093\\u306b\\u3061\\u306f'],
273-
['こんにちは', '\\u3053\\u3093\\u306b\\u3061\\u306f'],
297+
['こんにちは', '\\u3053\\u3093\\u306b\\u3061\\u306f']
274298
])('should escape key "%s" as "%s"', (key: string, expected: string) => {
275299
const result = properties.escapeKey(key)
276300
expect(result).toEqual(expected)
@@ -293,7 +317,7 @@ describe('The property value escaping', () => {
293317
['\\foo12\\', '\\\\foo12\\\\'],
294318
['\0\u0001', '\\u0000\\u0001'],
295319
['\u3053\u3093\u306B\u3061\u306F', '\\u3053\\u3093\\u306b\\u3061\\u306f'],
296-
['こんにちは', '\\u3053\\u3093\\u306b\\u3061\\u306f'],
320+
['こんにちは', '\\u3053\\u3093\\u306b\\u3061\\u306f']
297321
])('should escape value "%s" as "%s"', (key: string, expected: string) => {
298322
const result = properties.escapeValue(key)
299323
expect(result).toEqual(expected)

src/properties.ts

+17
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ export const get = (config: Properties, key: string): string | undefined => {
9494
return typeof rawValue === 'string' ? unescape(rawValue) : undefined
9595
}
9696

97+
/**
98+
* Loads all defined keys in the Object.
99+
*
100+
* If duplicate keys are found, last one is used.
101+
*
102+
* @param config Java properties set.
103+
*/
104+
export const toObject = (config: Properties): Record<string, string> => {
105+
const result: Record<string, string> = {}
106+
107+
for (const {key, rawValue} of listPairs(config.lines)) {
108+
result[key] = unescape(rawValue)
109+
}
110+
111+
return result
112+
}
113+
97114
/**
98115
* Loads all defined keys in the Map.
99116
*

0 commit comments

Comments
 (0)