Skip to content

Commit 164b627

Browse files
committed
fix: rewritten parser to handle all cases (except unicode)
1 parent a4cdae1 commit 164b627

File tree

3 files changed

+194
-148
lines changed

3 files changed

+194
-148
lines changed

fixtures/test-all.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ keyWith\Backslashes = This\ has ran\d\o\m backslashes
6969
encodedHelloInJapanese = \u3053\u3093\u306b\u3061\u306f
7070
# Using \u without being followed by four hexadecimal digits will throw an exception.
7171
# But with more modern file encodings like UTF-8, you can directly use supported characters.
72-
helloInJapanese = ?????
72+
helloInJapanese = こんにちは

src/properties.spec.ts

+22-34
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ describe('data access', () => {
4646

4747
beforeEach(() => {
4848
sample.lines = [
49-
'foo=bar',
49+
' foo0',
50+
'foo1=bar',
5051
'foo2:bar2',
5152
'foo3 bar3',
5253
' foo4 bar4 ',
5354
'foo5 = bar5',
5455
'# foo6 = bar6',
55-
'invalid_line',
5656
' ! foo7 = bar7',
5757
'foo8\\::bar8',
5858
'foo9\\==bar9',
@@ -78,10 +78,11 @@ describe('data access', () => {
7878
})
7979

8080
const samplePairs = [
81-
['foo', 'bar'],
81+
['foo0', ''],
82+
['foo1', 'bar'],
8283
['foo2', 'bar2'],
8384
['foo3', 'bar3'],
84-
['foo4', 'bar4'],
85+
['foo4', 'bar4 '],
8586
['foo5', 'bar5'],
8687
['foo8:', 'bar8'],
8788
['foo9=', 'bar9'],
@@ -101,35 +102,20 @@ describe('data access', () => {
101102
['foo23', 'bar23']
102103
]
103104

104-
it.each([
105-
['foo', 'bar'],
106-
['foo2', 'bar2'],
107-
['foo3', 'bar3'],
108-
['foo4', 'bar4'],
109-
['foo5', 'bar5'],
110-
['foo6', undefined],
111-
['foo7', undefined],
112-
['foo8:', 'bar8'],
113-
['foo9=', 'bar9'],
114-
['foo10=', 'bar10'],
115-
['foo11 ', 'bar11'],
116-
[' foo12', 'bar12'],
117-
['#foo13', 'bar13'],
118-
['!foo14#', 'bar14'],
119-
['foo15', '#bar15'],
120-
['foo16', 'bar16'],
121-
['foo17', 'bar17'],
122-
['f o o18', ' bar18'],
123-
['foo19\n', 'bar\t\f\r19\n'],
124-
['foo20', ''],
125-
['foo21', ''],
126-
['foo22', '\\'],
127-
['foo23', 'bar23']
128-
])('should get property "%s"', (key, expected) => {
105+
it.each(samplePairs)('should get property "%s"', (key, expected) => {
129106
const result = properties.get(sample, key)
130107
expect(result).toBe(expected)
131108
})
132109

110+
it.each([
111+
['foo6'],
112+
['foo7']
113+
])('should not get commented property "%s"', (key) => {
114+
const result = properties.get(sample, key)
115+
expect(result).toBeUndefined()
116+
})
117+
118+
133119
it('should return last value of duplicate key', () => {
134120
const config: properties.Properties = {
135121
lines: [
@@ -144,7 +130,7 @@ describe('data access', () => {
144130
})
145131

146132
it.each([
147-
['foo', 'bar', 'foo=bar'],
133+
['foo1', 'bar', 'foo1=bar'],
148134
['foo8:', 'bar8', 'foo8\\:=bar8'],
149135
['foo9=', 'bar9', 'foo9\\==bar9'],
150136
['foo10=', 'bar10', 'foo10\\==bar10'],
@@ -180,7 +166,8 @@ describe('data access', () => {
180166

181167
it('should replace key pairs', () => {
182168
const keys = [
183-
'foo',
169+
'foo0',
170+
'foo1',
184171
'foo2',
185172
'foo3',
186173
'foo4',
@@ -206,7 +193,8 @@ describe('data access', () => {
206193
keys.forEach(key => properties.set(sample, key, 'x'))
207194

208195
expect(sample.lines).toEqual([
209-
'foo=x',
196+
'foo0=x',
197+
'foo1=x',
210198
'foo2:x',
211199
'foo3 x',
212200
'foo4 x',
@@ -339,10 +327,10 @@ describe('data access', () => {
339327
})
340328

341329
it('should parse test file', async () => {
342-
const contents = await fs.readFile('../fixtures/test-all.properties', 'utf-8')
330+
const contents = await fs.readFile(require.resolve('../fixtures/test-all.properties'), 'utf-8')
343331

344332
// Parse
345-
const result = properties.toMap(properties.parse(contents))
333+
const result = properties.toObject(properties.parse(contents))
346334

347335
// Verify
348336
expect(result).toEqual({

0 commit comments

Comments
 (0)