Skip to content

Commit ba6f8eb

Browse files
committed
fix: more descriptive accessor method names
NOTE that this is technically breaking change, but since it is early (still unused) release, only patch version will be bumped.
1 parent 4cf1af7 commit ba6f8eb

File tree

3 files changed

+85
-77
lines changed

3 files changed

+85
-77
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import * as properties from 'js-java-properties'
6262
const props = properties.empty()
6363
props.lines.push('key1=value1', 'key2 = value2', 'key3: value3')
6464

65-
for (const {key, value} of properties.list(props)) {
65+
for (const {key, value} of properties.listProperties(props)) {
6666
console.log(`${key}=${value}`)
6767
// key1=value1
6868
// key2=value2
@@ -83,7 +83,7 @@ import * as properties from 'js-java-properties'
8383
const props = properties.empty()
8484
props.lines.push('key1=value1', 'key2 = value2', 'key3: value3')
8585

86-
console.log(properties.get(props, 'key2'))
86+
console.log(properties.getProperty(props, 'key2'))
8787
// 'value2'
8888
```
8989

@@ -115,19 +115,19 @@ import * as properties from 'js-java-properties'
115115
const props = properties.empty()
116116
props.lines.push('key1=value1', 'key2 = value2', 'key3: value3')
117117

118-
properties.set(props, 'key2', 'new-value')
118+
properties.setProperty(props, 'key2', 'new-value')
119119
console.log(properties.stringify(props))
120120
// 'key1=value1\nkey2 = new-value\nkey3: value3\n'
121121

122-
properties.set(props, 'new-key', 'new-value')
122+
properties.setProperty(props, 'new-key', 'new-value')
123123
console.log(properties.stringify(props))
124124
// 'key1=value1\nkey2 = new-value\nkey3: value3\nnew-key=new-value\n'
125125

126-
properties.set(props, 'new-key', 'new-value', {separator: ':'})
126+
properties.setProperty(props, 'new-key', 'new-value', {separator: ':'})
127127
console.log(properties.stringify(props))
128128
// 'key1=value1\nkey2 = new-value\nkey3: value3\nnew-key:new-value\n'
129129

130-
properties.set(props, 'key3', undefined)
130+
properties.setProperty(props, 'key3', undefined)
131131
console.log(properties.stringify(props))
132132
// 'key1=value1\nkey2 = new-value\n'
133133
```
@@ -142,7 +142,7 @@ import * as properties from 'js-java-properties'
142142
const props = properties.empty()
143143
props.lines.push('key1=value1', 'key2 = value2', 'key3: value3')
144144

145-
properties.remove(props, 'key2')
145+
properties.removeProperty(props, 'key2')
146146
console.log(properties.stringify(props))
147147
// 'key1=value1\nkey3: value3\n'
148148
```

src/properties.spec.ts

+60-60
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,16 @@ describe('data access', () => {
112112
['foo23', 'bar23']
113113
]
114114

115-
describe('get value', () => {
115+
describe('getProperty', () => {
116116
it.each(samplePairs)('should get property "%s"', (key, expected) => {
117-
const result = properties.get(sample, key)
117+
const result = properties.getProperty(sample, key)
118118
expect(result).toBe(expected)
119119
})
120120

121121
it.each([['foo6'], ['foo7']])(
122122
'should not get commented property "%s"',
123123
key => {
124-
const result = properties.get(sample, key)
124+
const result = properties.getProperty(sample, key)
125125
expect(result).toBeUndefined()
126126
}
127127
)
@@ -131,7 +131,7 @@ describe('data access', () => {
131131
lines: ['key1=foo1', 'key2=foo2', 'key1=foo3']
132132
}
133133

134-
const result = properties.get(config, 'key1')
134+
const result = properties.getProperty(config, 'key1')
135135
expect(result).toBe('foo3')
136136
})
137137

@@ -140,7 +140,7 @@ describe('data access', () => {
140140
lines: ['foo\\u23a=bar']
141141
}
142142

143-
expect(() => properties.get(config, 'foo')).toThrowError()
143+
expect(() => properties.getProperty(config, 'foo')).toThrowError()
144144
})
145145

146146
it.each([['foo=bar\\u23a'], ['foo=bar\\u23ax5']])(
@@ -150,7 +150,7 @@ describe('data access', () => {
150150
lines: [line]
151151
}
152152

153-
expect(() => properties.get(config, 'foo')).toThrowError()
153+
expect(() => properties.getProperty(config, 'foo')).toThrowError()
154154
}
155155
)
156156

@@ -165,12 +165,12 @@ describe('data access', () => {
165165
lines: [line]
166166
}
167167

168-
const result = properties.get(config, 'foo')
168+
const result = properties.getProperty(config, 'foo')
169169
expect(result).toBe(value)
170170
})
171171
})
172172

173-
describe('set value', () => {
173+
describe('setProperty', () => {
174174
it.each([
175175
['foo1', 'bar', 'foo1=bar'],
176176
['foo8:', 'bar8', 'foo8\\:=bar8'],
@@ -187,7 +187,7 @@ describe('data access', () => {
187187
['foo22', '\\', 'foo22=\\\\']
188188
])('should format key pair for "%s"', (key, value, expected) => {
189189
const config = properties.empty()
190-
properties.set(config, key, value)
190+
properties.setProperty(config, key, value)
191191
expect(config.lines).toEqual([expected])
192192
})
193193

@@ -202,7 +202,7 @@ describe('data access', () => {
202202
const config: properties.Properties = {
203203
lines: [line]
204204
}
205-
properties.set(config, 'a', 'b')
205+
properties.setProperty(config, 'a', 'b')
206206
expect(config.lines).toEqual([line, expected])
207207
})
208208

@@ -232,7 +232,7 @@ describe('data access', () => {
232232
'foo22',
233233
'foo23'
234234
]
235-
keys.forEach(key => properties.set(sample, key, 'x'))
235+
keys.forEach(key => properties.setProperty(sample, key, 'x'))
236236

237237
expect(sample.lines).toEqual([
238238
'foo0=x',
@@ -268,7 +268,7 @@ describe('data access', () => {
268268
lines: ['key1=foo1', 'key2=foo2']
269269
}
270270

271-
properties.set(config, 'key1', 'test', {separator: ': '})
271+
properties.setProperty(config, 'key1', 'test', {separator: ': '})
272272
expect(config.lines).toEqual(['key1: test', 'key2=foo2'])
273273
})
274274

@@ -277,25 +277,25 @@ describe('data access', () => {
277277
lines: ['key1=foo1', 'key2=foo2', 'key1=foo3']
278278
}
279279

280-
properties.set(config, 'key1', 'test')
280+
properties.setProperty(config, 'key1', 'test')
281281
expect(config.lines).toEqual(['key1=test', 'key2=foo2'])
282282
})
283-
})
284283

285-
describe('remove value', () => {
286284
it('should remove existing key with set undefined', () => {
287285
const config: properties.Properties = {
288286
lines: ['foo=bar']
289287
}
290-
properties.set(config, 'foo', undefined)
288+
properties.setProperty(config, 'foo', undefined)
291289
expect(config.lines).toEqual([])
292290
})
291+
})
293292

293+
describe('removeProperty', () => {
294294
it('should remove existing key with remove', () => {
295295
const config: properties.Properties = {
296296
lines: ['foo=bar']
297297
}
298-
properties.remove(config, 'foo')
298+
properties.removeProperty(config, 'foo')
299299
expect(config.lines).toEqual([])
300300
})
301301

@@ -304,14 +304,14 @@ describe('data access', () => {
304304
lines: ['key1=foo1', 'key2=foo2', 'key1=foo3']
305305
}
306306

307-
properties.remove(config, 'key1')
307+
properties.removeProperty(config, 'key1')
308308
expect(config.lines).toEqual(['key2=foo2'])
309309
})
310310
})
311311

312-
describe('list', () => {
312+
describe('listProperties', () => {
313313
it('should list all key-value pairs', () => {
314-
const result = [...properties.list(sample)]
314+
const result = [...properties.listProperties(sample)]
315315
const resultAsArrays = result.map(({key, value}) => [key, value])
316316

317317
expect(resultAsArrays).toEqual(samplePairs)
@@ -322,7 +322,7 @@ describe('data access', () => {
322322
lines: ['foo=bar1', 'foo=bar2']
323323
}
324324

325-
const result = [...properties.list(config)]
325+
const result = [...properties.listProperties(config)]
326326
expect(result).toEqual([
327327
{key: 'foo', value: 'bar1'},
328328
{key: 'foo', value: 'bar2'}
@@ -368,46 +368,46 @@ describe('data access', () => {
368368
['c', 'd']
369369
])
370370
})
371+
})
372+
373+
it('should parse test file', async () => {
374+
const contents = await fs.readFile(
375+
require.resolve('../fixtures/test-all.properties'),
376+
'utf-8'
377+
)
371378

372-
it('should parse test file', async () => {
373-
const contents = await fs.readFile(
374-
require.resolve('../fixtures/test-all.properties'),
375-
'utf-8'
376-
)
377-
378-
// Parse
379-
const result = properties.toObject(properties.parse(contents))
380-
381-
// Verify
382-
expect(result).toEqual({
383-
'': 'So does this line.',
384-
category: 'file format',
385-
duplicateKey: 'second',
386-
empty: '',
387-
encodedHelloInJapanese: 'こんにちは',
388-
evenKey: 'This is on one line\\',
389-
'evenLikeThis\\': '',
390-
hello: 'hello',
391-
helloInJapanese: 'こんにちは',
392-
こんにちは: 'hello',
393-
keyWithBackslashes: 'This has random backslashes',
394-
'keyWithDelimiters:= ':
395-
'This is the value for the key "keyWithDelimiters:= "',
396-
'keyWitheven\\': 'this colon is not escaped',
397-
language: 'English',
398-
multiline: 'This line continues on 3 lines',
399-
multilineKey: 'this is a multiline key',
400-
noWhiteSpace:
401-
'The key will be "noWhiteSpace" without any whitespace. ',
402-
oddKey: 'This is line one and\\# This is line two',
403-
orLikeThis: '',
404-
path: 'c:\\wiki\\templates',
405-
topic: '.properties file',
406-
valueWithEscapes:
407-
'This is a newline\n, a carriage return\r, a tab\t and a formfeed\f.',
408-
website: 'https://en.wikipedia.org/',
409-
welcome: 'Welcome to Wikipedia! '
410-
})
379+
// Parse
380+
const result = properties.toObject(properties.parse(contents))
381+
382+
// Verify
383+
expect(result).toEqual({
384+
'': 'So does this line.',
385+
category: 'file format',
386+
duplicateKey: 'second',
387+
empty: '',
388+
encodedHelloInJapanese: 'こんにちは',
389+
evenKey: 'This is on one line\\',
390+
'evenLikeThis\\': '',
391+
hello: 'hello',
392+
helloInJapanese: 'こんにちは',
393+
'\u3053\u3093\u306B\u3061\u306F': 'hello',
394+
keyWithBackslashes: 'This has random backslashes',
395+
'keyWithDelimiters:= ':
396+
'This is the value for the key "keyWithDelimiters:= "',
397+
'keyWitheven\\': 'this colon is not escaped',
398+
language: 'English',
399+
multiline: 'This line continues on 3 lines',
400+
multilineKey: 'this is a multiline key',
401+
noWhiteSpace:
402+
'The key will be "noWhiteSpace" without any whitespace. ',
403+
oddKey: 'This is line one and\\# This is line two',
404+
orLikeThis: '',
405+
path: 'c:\\wiki\\templates',
406+
topic: '.properties file',
407+
valueWithEscapes:
408+
'This is a newline\n, a carriage return\r, a tab\t and a formfeed\f.',
409+
website: 'https://en.wikipedia.org/',
410+
welcome: 'Welcome to Wikipedia! '
411411
})
412412
})
413413
})

src/properties.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const stringify = (config: Properties): string => {
8686
*
8787
* @param config Java properties set.
8888
*/
89-
export function* list(config: Properties): Generator<KeyValuePair> {
89+
export function* listProperties(config: Properties): Generator<KeyValuePair> {
9090
for (const {key, value} of listPairs(config.lines)) {
9191
yield {key, value}
9292
}
@@ -102,7 +102,10 @@ export function* list(config: Properties): Generator<KeyValuePair> {
102102
* @param key Key name.
103103
* @return Found value, or undefined. Value is properly unescaped.
104104
*/
105-
export const get = (config: Properties, key: string): string | undefined => {
105+
export const getProperty = (
106+
config: Properties,
107+
key: string
108+
): string | undefined => {
106109
let value: string | undefined = undefined
107110

108111
// Find last value
@@ -158,7 +161,12 @@ export const toMap = (config: Properties): Map<string, string> => {
158161
* @param sep Separator, cannot be empty. Valid chars are ` :=`.
159162
* @param escapeUnicode Enable/disable unicode escaping.
160163
*/
161-
const formatLine = (key: string, value: string, sep: string, escapeUnicode: boolean) =>
164+
const formatLine = (
165+
key: string,
166+
value: string,
167+
sep: string,
168+
escapeUnicode: boolean
169+
) =>
162170
`${escapeKey(key, escapeUnicode)}${sep}${escapeValue(value, escapeUnicode)}`
163171

164172
/**
@@ -169,26 +177,26 @@ const formatLine = (key: string, value: string, sep: string, escapeUnicode: bool
169177
* @param value New value. If undefined or null, key will be removed.
170178
* @param options Optionally override set behavior.
171179
*/
172-
export const set = (
180+
export const setProperty = (
173181
config: Properties,
174182
key: string,
175183
value: string | undefined | null,
176184
options?: {separator?: string}
177185
): void => {
178186
const escapeUnicode = true
179-
let lastSep = '='
187+
let sep = options?.separator || '='
180188
let found = false
181189

182190
// Find all entries
183191
for (const entry of listPairs(config.lines)) {
184192
// Remember separator
185-
if (entry.sep) lastSep = entry.sep
193+
if (!options?.separator && entry.sep) sep = entry.sep
186194

187195
// If found, either replace or remove
188196
if (key === entry.key) {
189197
const items =
190198
!found && typeof value === 'string'
191-
? [formatLine(key, value, options?.separator || lastSep, escapeUnicode)]
199+
? [formatLine(key, value, sep, escapeUnicode)]
192200
: []
193201

194202
config.lines.splice(entry.start, entry.len, ...items)
@@ -198,7 +206,7 @@ export const set = (
198206

199207
// Not found, append
200208
if (!found && typeof value === 'string') {
201-
config.lines.push(formatLine(key, value, options?.separator || lastSep, escapeUnicode))
209+
config.lines.push(formatLine(key, value, sep, escapeUnicode))
202210
}
203211
}
204212

@@ -210,8 +218,8 @@ export const set = (
210218
* @param config Java properties set.
211219
* @param key Key name.
212220
*/
213-
export const remove = (config: Properties, key: string): void =>
214-
set(config, key, undefined)
221+
export const removeProperty = (config: Properties, key: string): void =>
222+
setProperty(config, key, undefined)
215223

216224
/**
217225
* Character iterator over lines of chars.

0 commit comments

Comments
 (0)