Skip to content

Commit 29f2cea

Browse files
committed
refactor: add dry newUnicodeError function
1 parent 746a566 commit 29f2cea

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/properties.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {escapeKey, escapeValue} from './escape'
66
export type Properties = {
77
/**
88
* Plain text unparsed lines.
9+
*
10+
* Must not contain newline (`\r` and `\n`) characters.
911
*/
1012
lines: string[]
1113
}
@@ -25,14 +27,14 @@ export type KeyValuePair = {
2527
}
2628

2729
/**
28-
* Returns an empty object.
30+
* Byte-order mark.
2931
*/
30-
export const empty = (): Properties => ({lines: []})
32+
export const BOM = 0xfeff
3133

3234
/**
33-
* Byte-order mark.
35+
* Returns an empty object.
3436
*/
35-
export const BOM = 0xfeff
37+
export const empty = (): Properties => ({lines: []})
3638

3739
/**
3840
* Parses java properties file contents.
@@ -299,7 +301,7 @@ function* listPairs(lines: string[]): Generator<{
299301
if (state.unicode) {
300302
// Handle incomplete sequence
301303
if (char === 'EOL') {
302-
throw new Error(`Invalid unicode sequence at line ${lineNumber}`)
304+
throw newUnicodeError(lineNumber)
303305
}
304306

305307
// Append and consume until it has correct length
@@ -491,9 +493,13 @@ const unescapeControlChar = (c: string): string => {
491493
}
492494
}
493495

494-
const parseUnicode = (sequence: string, line: number): string => {
496+
const parseUnicode = (sequence: string, lineNumber: number): string => {
495497
if (!sequence.match(/^0x[\da-fA-F]{4}$/)) {
496-
throw new Error(`Invalid unicode sequence at line ${line}`)
498+
throw newUnicodeError(lineNumber)
497499
}
498500
return String.fromCodePoint(parseInt(sequence, 16))
499501
}
502+
503+
const newUnicodeError = (lineNumber: number): Error => {
504+
return new Error(`Invalid unicode sequence at line ${lineNumber}`)
505+
}

0 commit comments

Comments
 (0)