java-props
Advanced tools
Comparing version 2.1.1 to 2.1.2
@@ -8,3 +8,4 @@ export interface Properties { | ||
parse: typeof parse; | ||
stringify: typeof stringify; | ||
}; | ||
export default _default; |
@@ -66,3 +66,4 @@ "use strict"; | ||
parse, | ||
stringify, | ||
}; | ||
//# sourceMappingURL=java-props.js.map |
{ | ||
"name": "java-props", | ||
"version": "2.1.1", | ||
"version": "2.1.2", | ||
"description": "Read Java .properties files (using the same specification), without useless additional features.", | ||
@@ -5,0 +5,0 @@ "author": "Nathan Poirier <nathan@poirier.io>", |
@@ -55,3 +55,3 @@ import {decodeLine, encodeLine, LineReader} from './utils'; | ||
export function stringify(props: Properties): string { // TODO: Add unit-tests | ||
export function stringify(props: Properties): string { | ||
let str = ''; | ||
@@ -69,2 +69,3 @@ for (const key in props) { | ||
parse, | ||
stringify, | ||
}; |
@@ -15,3 +15,3 @@ /* | ||
modules: [CommonJavaProps, CommonJavaPropsW, CommonJavaPropsR], | ||
methods: ['parse'], | ||
methods: ['parse', 'stringify'], | ||
}; | ||
@@ -18,0 +18,0 @@ |
import fs from 'fs'; | ||
import JavaProps from '../src/java-props'; | ||
import {convertLine} from '../src/utils'; | ||
import {decodeLine, encodeLine} from '../src/utils'; | ||
@@ -8,3 +8,3 @@ describe('parse', () => { | ||
const res = JSON.parse(fs.readFileSync(__dirname + '/test.properties-result.json', 'utf8')); | ||
const props = await JavaProps.parse(fs.readFileSync(__dirname + '/test.properties', 'utf8')); | ||
const props = JavaProps.parse(fs.readFileSync(__dirname + '/test.properties', 'utf8')); | ||
expect(props).toEqual(res); | ||
@@ -33,14 +33,46 @@ }); | ||
describe('utils.convertLine', () => { | ||
const converts: any = { | ||
t: '\t', | ||
r: '\r', | ||
n: '\n', | ||
f: '\f', | ||
describe('stringify', () => { | ||
const expectConvertBack = (props: any) => { | ||
const newProps = JavaProps.parse(JavaProps.stringify(props)); | ||
expect(newProps).toEqual(props); | ||
}; | ||
it('convert back test.properties', async () => { | ||
expectConvertBack(JavaProps.parse(fs.readFileSync(__dirname + '/test.properties', 'utf8'))); | ||
}); | ||
it('must handle spacings', async () => { | ||
expectConvertBack({' key': 'value'}); | ||
expectConvertBack({'key': ' value'}); | ||
expectConvertBack({'key ': 'value'}); | ||
expectConvertBack({'key': 'value '}); | ||
expectConvertBack({' ke:y= ': ' = \\nval\\ue\n '}); | ||
}); | ||
it('must skip non-own properties', async () => { | ||
const obj = {'key': 'value'}; | ||
(Object.prototype as any).notAKey = 'x'; | ||
expect(JavaProps.stringify(obj)).toBe('key: value\n'); | ||
}); | ||
it('must handle null-prototype objects', async () => { | ||
const obj = Object.create(null); | ||
obj.key = 'value'; | ||
expect(JavaProps.stringify(obj)).toBe('key: value\n'); | ||
}); | ||
}); | ||
const converts: any = { | ||
t: '\t', | ||
r: '\r', | ||
n: '\n', | ||
f: '\f', | ||
}; | ||
describe('utils.decodeLine', () => { | ||
for (const char in converts) { | ||
if (converts.hasOwnProperty(char)) { | ||
const repl = converts[char]; | ||
it('must convert \\' + char, () => { | ||
expect(convertLine('abc\\' + char + 'def\\' + char + 'ghi')).toBe('abc' + repl + 'def' + repl + 'ghi'); | ||
it('must decode \\' + char, () => { | ||
expect(decodeLine('abc\\' + char + 'def\\' + char + 'ghi')).toBe('abc' + repl + 'def' + repl + 'ghi'); | ||
}); | ||
@@ -51,23 +83,72 @@ } | ||
it('must remove useless escapes', () => { | ||
expect(convertLine('a\\bc\\def\\\\')).toBe('abcdef\\'); | ||
expect(decodeLine('a\\bc\\def\\\\')).toBe('abcdef\\'); | ||
}); | ||
it('must replace \\uxxxx', () => { | ||
expect(convertLine('\\u0058 \\u2764')).toBe('X ❤'); | ||
expect(convertLine('\\u00580')).toBe('X0'); | ||
expect(convertLine('\\u0058A\\u2764Test')).toBe('XA❤Test'); | ||
expect(convertLine('\\u002F \\u002f')).toBe('/ /'); | ||
expect(decodeLine('\\u0058 \\u2764')).toBe('X ❤'); | ||
expect(decodeLine('\\u00580')).toBe('X0'); | ||
expect(decodeLine('\\u0058A\\u2764Test')).toBe('XA❤Test'); | ||
expect(decodeLine('\\u002F \\u002f')).toBe('/ /'); | ||
}); | ||
it('must throw on malformed \\uxxxx', () => { | ||
expect(() => convertLine('\\u')).toThrow(); | ||
expect(() => convertLine('\\u58')).toThrow(); | ||
expect(() => convertLine('\\u27g4')).toThrow(); | ||
expect(() => decodeLine('\\u')).toThrow(); | ||
expect(() => decodeLine('\\u58')).toThrow(); | ||
expect(() => decodeLine('\\u27g4')).toThrow(); | ||
}); | ||
it('must not replace escaped', () => { | ||
expect(convertLine('test\\nand\\\\n \\\\\\n')).toBe('test\nand\\n \\\n'); | ||
expect(convertLine('abc \\\\u0000')).toBe('abc \\u0000'); | ||
expect(convertLine('abc \\\\uxxxx')).toBe('abc \\uxxxx'); | ||
expect(decodeLine('test\\nand\\\\n \\\\\\n')).toBe('test\nand\\n \\\n'); | ||
expect(decodeLine('abc \\\\u0000')).toBe('abc \\u0000'); | ||
expect(decodeLine('abc \\\\uxxxx')).toBe('abc \\uxxxx'); | ||
}); | ||
}); | ||
const origEncodeLine = encodeLine; | ||
for (const isKey of [false, true]) { | ||
const encodeLine = (line: string) => origEncodeLine(line, isKey); | ||
describe('utils.encodeLine (isKey=' + isKey + ')', () => { | ||
for (const char in converts) { | ||
if (converts.hasOwnProperty(char)) { | ||
const repl = converts[char]; | ||
it('must encode \\' + char, () => { | ||
expect(encodeLine('abc' + repl + 'def' + repl + 'ghi')).toBe('abc\\' + char + 'def\\' + char + 'ghi'); | ||
}); | ||
} | ||
} | ||
it('must escape special chars', () => { | ||
expect(encodeLine('!# :=\\')).toBe(isKey ? '\\!\\#\\ \\:\\=\\\\' : '!# :=\\\\'); | ||
}); | ||
if (!isKey) { | ||
it('must escape beginning space', () => { | ||
expect(encodeLine(' abc def ghi jkl ')).toBe('\\ abc def ghi jkl '); | ||
}); | ||
} | ||
it('must not escape normal chars', () => { | ||
let str = ''; | ||
for (let cc = 33; cc < 127; ++cc) { | ||
const c = String.fromCharCode(cc); | ||
if (c === '\\' || (isKey && (c === '!' || c === '#' || c === ' ' || c === ':' || c === '='))) { | ||
continue; | ||
} | ||
str += c; | ||
} | ||
expect(encodeLine(str)).toBe(str); | ||
}); | ||
it('must encode others unicode chars', () => { | ||
expect(encodeLine('\0')).toBe('\\u0000'); | ||
expect(encodeLine('\u0014')).toBe('\\u0014'); | ||
expect(encodeLine('\u007F')).toBe('\\u007F'); | ||
expect(encodeLine('é')).toBe('\\u00E9'); | ||
expect(encodeLine('👑')).toBe('\\uD83D\\uDC51'); | ||
expect(encodeLine(String.fromCharCode(15))).toBe('\\u000F'); | ||
expect(encodeLine(String.fromCharCode(255))).toBe('\\u00FF'); | ||
expect(encodeLine(String.fromCharCode(4095))).toBe('\\u0FFF'); | ||
expect(encodeLine(String.fromCharCode(65535))).toBe('\\uFFFF'); | ||
}); | ||
}); | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45668
949