Skip to content

Commit

Permalink
feat: added toObject function
Browse files Browse the repository at this point in the history
  • Loading branch information
mdvorak committed Mar 18, 2023
1 parent a1d9a97 commit 06b4077
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 19 deletions.
62 changes: 43 additions & 19 deletions src/properties.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,29 +227,53 @@ describe('data access', () => {
expect(config.lines).toEqual([])
})

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

expect(resultAsArrays).toEqual(samplePairs)
expect(resultAsArrays).toEqual(samplePairs)
})
})

it('should return all keys toMap', () => {
const result = properties.toMap(sample)
expect([...result.entries()]).toEqual(samplePairs)
describe('toObject', () => {
it('should return all pairs', () => {
const result = properties.toObject(sample)
expect(Object.entries(result)).toEqual(samplePairs)
})

it('should return last value of duplicate key', () => {
const config: properties.Properties = {
lines: ['foo=bar1', 'a=b', 'foo=bar2', 'foo=bar3', 'c=d']
}

const result = properties.toObject(config)
expect(Object.entries(result)).toEqual([
['foo', 'bar3'],
['a', 'b'],
['c', 'd']
])
})
})

it('should return last value of duplicate key', () => {
const config: properties.Properties = {
lines: ['foo=bar1', 'a=b', 'foo=bar2', 'foo=bar3', 'c=d']
}
describe('toMap', () => {
it('should return all pairs', () => {
const result = properties.toMap(sample)
expect([...result.entries()]).toEqual(samplePairs)
})

const result = properties.toMap(config)
expect([...result.entries()]).toEqual([
['foo', 'bar3'],
['a', 'b'],
['c', 'd']
])
it('should return last value of duplicate key', () => {
const config: properties.Properties = {
lines: ['foo=bar1', 'a=b', 'foo=bar2', 'foo=bar3', 'c=d']
}

const result = properties.toMap(config)
expect([...result.entries()]).toEqual([
['foo', 'bar3'],
['a', 'b'],
['c', 'd']
])
})
})
})

Expand All @@ -270,7 +294,7 @@ describe('The property key escaping', () => {
['\\foo12\\', '\\\\foo12\\\\'],
['\0\u0001', '\\u0000\\u0001'],
['\u3053\u3093\u306B\u3061\u306F', '\\u3053\\u3093\\u306b\\u3061\\u306f'],
['こんにちは', '\\u3053\\u3093\\u306b\\u3061\\u306f'],
['こんにちは', '\\u3053\\u3093\\u306b\\u3061\\u306f']
])('should escape key "%s" as "%s"', (key: string, expected: string) => {
const result = properties.escapeKey(key)
expect(result).toEqual(expected)
Expand All @@ -293,7 +317,7 @@ describe('The property value escaping', () => {
['\\foo12\\', '\\\\foo12\\\\'],
['\0\u0001', '\\u0000\\u0001'],
['\u3053\u3093\u306B\u3061\u306F', '\\u3053\\u3093\\u306b\\u3061\\u306f'],
['こんにちは', '\\u3053\\u3093\\u306b\\u3061\\u306f'],
['こんにちは', '\\u3053\\u3093\\u306b\\u3061\\u306f']
])('should escape value "%s" as "%s"', (key: string, expected: string) => {
const result = properties.escapeValue(key)
expect(result).toEqual(expected)
Expand Down
17 changes: 17 additions & 0 deletions src/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ export const get = (config: Properties, key: string): string | undefined => {
return typeof rawValue === 'string' ? unescape(rawValue) : undefined
}

/**
* Loads all defined keys in the Object.
*
* If duplicate keys are found, last one is used.
*
* @param config Java properties set.
*/
export const toObject = (config: Properties): Record<string, string> => {
const result: Record<string, string> = {}

for (const {key, rawValue} of listPairs(config.lines)) {
result[key] = unescape(rawValue)
}

return result
}

/**
* Loads all defined keys in the Map.
*
Expand Down

0 comments on commit 06b4077

Please sign in to comment.