java-props
Advanced tools
Comparing version 2.0.4 to 2.1.0
@@ -5,2 +5,3 @@ export interface Properties { | ||
export declare function parse(str: string): Properties; | ||
export declare function stringify(props: Properties): string; | ||
declare const _default: { | ||
@@ -7,0 +8,0 @@ parse: typeof parse; |
@@ -46,4 +46,4 @@ "use strict"; | ||
} | ||
const key = utils_1.convertLine(line.substring(0, keyLen)); | ||
const value = utils_1.convertLine(line.substring(valueStart)); | ||
const key = utils_1.decodeLine(line.substring(0, keyLen)); | ||
const value = utils_1.decodeLine(line.substring(valueStart)); | ||
result[key] = value; | ||
@@ -54,2 +54,13 @@ } | ||
exports.parse = parse; | ||
function stringify(props) { | ||
let str = ''; | ||
for (const key in props) { | ||
if (Object.prototype.hasOwnProperty.call(props, key)) { | ||
const value = props[key]; | ||
str += utils_1.encodeLine(key, true) + ': ' + utils_1.encodeLine(value) + '\n'; | ||
} | ||
} | ||
return str; | ||
} | ||
exports.stringify = stringify; | ||
exports.default = { | ||
@@ -56,0 +67,0 @@ parse, |
@@ -1,2 +0,7 @@ | ||
export declare function convertLine(line: string): string; | ||
export declare function decodeLine(line: string): string; | ||
export declare function encodeLine(line: string, isKey?: boolean): string; | ||
/** | ||
* @deprecated Use {@link #decodeLine}. | ||
*/ | ||
export declare const convertLine: typeof decodeLine; | ||
export declare class LineReader { | ||
@@ -3,0 +8,0 @@ private readonly str; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const CONVERT_RXP = /(?:\\u(.{0,4})|\\(.?))/g; | ||
const UNICODE_RXP = /^[0-9a-fA-F]{4}$/; | ||
function convertLine(line) { | ||
return line.replace(CONVERT_RXP, (_, unicode, char) => { | ||
const DECODE_PATTERN = /(?:\\u(.{0,4})|\\(.?))/g; | ||
const UNICODE_PATTERN = /^[0-9a-fA-F]{4}$/; | ||
const ENCODE_PATTERN = /(?:[\u0000-\u001F\\\u007F-\uFFFF])/g; | ||
const ENCODE_KEY_PATTERN = /(?:[\u0000-\u0020!#:=\\\u007F-\uFFFF])/g; // ENCODE_PATTERN with separators + comments | ||
function decodeLine(line) { | ||
return line.replace(DECODE_PATTERN, (_, unicode, char) => { | ||
if (unicode !== undefined) { | ||
if (!unicode.match(UNICODE_RXP)) { | ||
if (!unicode.match(UNICODE_PATTERN)) { | ||
throw new Error('Malformed \\uxxxx encoding.'); | ||
@@ -31,3 +33,44 @@ } | ||
} | ||
exports.convertLine = convertLine; | ||
exports.decodeLine = decodeLine; | ||
function encodeLine(line, isKey) { | ||
let str = line.replace(isKey ? ENCODE_KEY_PATTERN : ENCODE_PATTERN, (c) => { | ||
if (c === '\t') { | ||
return '\\t'; | ||
} | ||
else if (c === '\r') { | ||
return '\\r'; | ||
} | ||
else if (c === '\n') { | ||
return '\\n'; | ||
} | ||
else if (c === '\f') { | ||
return '\\f'; | ||
} | ||
else if (c >= ' ' && c <= '~') { | ||
return '\\' + c; | ||
} | ||
else { | ||
const code = c.charCodeAt(0); | ||
if (code < 16) | ||
return '\\u000' + code.toString(16).toUpperCase(); | ||
if (code < 256) | ||
return '\\u00' + code.toString(16).toUpperCase(); | ||
if (code < 4096) | ||
return '\\u0' + code.toString(16).toUpperCase(); | ||
return '\\u' + code.toString(16).toUpperCase(); | ||
} | ||
}); | ||
if (!isKey) { | ||
const c = str.charAt(0); | ||
if (c === ' ' || c === '\t' || c === '\f') { | ||
str = '\\' + str; | ||
} | ||
} | ||
return str; | ||
} | ||
exports.encodeLine = encodeLine; | ||
/** | ||
* @deprecated Use {@link #decodeLine}. | ||
*/ | ||
exports.convertLine = decodeLine; | ||
class LineReader { | ||
@@ -34,0 +77,0 @@ constructor(str) { |
@@ -49,2 +49,15 @@ /** | ||
}, | ||
/** | ||
* Convert a JavaScript object to the corresponding .properties string. | ||
* | ||
* @param {Object} props The JavaScript object to convert | ||
* @return {String} The .properties string corresponding to the given JavaScript object | ||
* @example | ||
* const str = javaProps.stringify({'foo': 'Hello', 'bar': 'World'}); | ||
* console.log(str); | ||
* // "foo: Hello\nbar: World\n" | ||
*/ | ||
stringify: function (props) { | ||
}, | ||
}; |
{ | ||
"name": "java-props", | ||
"version": "2.0.4", | ||
"version": "2.1.0", | ||
"description": "Read Java .properties files (using the same specification), without useless additional features.", | ||
@@ -31,6 +31,6 @@ "author": "Nathan Poirier <nathan@poirier.io>", | ||
"@types/jest": "^24.0.18", | ||
"@types/node": "^12.7.5", | ||
"@types/node": "^12.7.11", | ||
"jest": "^24.9.0", | ||
"np": "^5.1.0", | ||
"rimraf": "^2.6.3", | ||
"rimraf": "^3.0.0", | ||
"ts-jest": "^24.1.0", | ||
@@ -37,0 +37,0 @@ "tslint": "^5.20.0", |
@@ -40,6 +40,4 @@ # java-props · [![Build Status](https://travis-ci.org/nathan818fr/node-java-props.svg?branch=master)](https://travis-ci.org/nathan818fr/node-java-props) [![codecov](https://codecov.io/gh/nathan818fr/node-java-props/branch/master/graph/badge.svg)](https://codecov.io/gh/nathan818fr/node-java-props) [![npm version](https://badge.fury.io/js/java-props.svg)](https://badge.fury.io/js/java-props) | ||
<!-- jsdoc2md start --> | ||
<a name="javaProps"></a> | ||
### javaProps | ||
**Example** | ||
**Example** | ||
```js | ||
@@ -52,2 +50,3 @@ const javaProps = require('java-props'); | ||
* [.parseFile(path, [encoding])](#javaProps.parseFile) ⇒ <code>Promise.<Object></code> | ||
* [.stringify(props)](#javaProps.stringify) ⇒ <code>String</code> | ||
@@ -59,3 +58,3 @@ <a name="javaProps.parse"></a> | ||
**Returns**: <code>Object</code> - The [Object](Object) corresponding to the given string | ||
**Returns**: <code>Object</code> - The [Object](Object) corresponding to the given string | ||
@@ -66,3 +65,3 @@ | Param | Type | Description | | ||
**Example** | ||
**Example** | ||
```js | ||
@@ -78,3 +77,3 @@ const props = javaProps.parse('foo=Hello\nbar=World'); | ||
**Returns**: <code>Promise.<Object></code> - The [Object](Object) corresponding to the given string | ||
**Returns**: <code>Promise.<Object></code> - The [Object](Object) corresponding to the given string | ||
@@ -86,3 +85,3 @@ | Param | Type | Default | Description | | ||
**Example** | ||
**Example** | ||
```js | ||
@@ -108,2 +107,19 @@ javaProps.parseFile('./foobar.properties').then((props) => { | ||
``` | ||
<a name="javaProps.stringify"></a> | ||
#### javaProps.stringify(props) ⇒ <code>String</code> | ||
Convert a JavaScript object to the corresponding .properties string. | ||
**Returns**: <code>String</code> - The .properties string corresponding to the given JavaScript object | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| props | <code>Object</code> | The JavaScript object to convert | | ||
**Example** | ||
```js | ||
const str = javaProps.stringify({'foo': 'Hello', 'bar': 'World'}); | ||
console.log(str); | ||
// "foo: Hello\nbar: World\n" | ||
``` | ||
<!-- jsdoc2md end --> | ||
@@ -110,0 +126,0 @@ |
@@ -1,2 +0,2 @@ | ||
import {convertLine, LineReader} from './utils'; | ||
import {decodeLine, encodeLine, LineReader} from './utils'; | ||
@@ -48,4 +48,4 @@ export interface Properties { | ||
const key = convertLine(line.substring(0, keyLen)); | ||
const value = convertLine(line.substring(valueStart)); | ||
const key = decodeLine(line.substring(0, keyLen)); | ||
const value = decodeLine(line.substring(valueStart)); | ||
result[key] = value; | ||
@@ -56,4 +56,15 @@ } | ||
export function stringify(props: Properties): string { // TODO: Add unit-tests | ||
let str = ''; | ||
for (const key in props) { | ||
if (Object.prototype.hasOwnProperty.call(props, key)) { | ||
const value = props[key]; | ||
str += encodeLine(key, true) + ': ' + encodeLine(value) + '\n'; | ||
} | ||
} | ||
return str; | ||
} | ||
export default { | ||
parse, | ||
}; |
@@ -1,8 +0,11 @@ | ||
const CONVERT_RXP = /(?:\\u(.{0,4})|\\(.?))/g; | ||
const UNICODE_RXP = /^[0-9a-fA-F]{4}$/; | ||
const DECODE_PATTERN = /(?:\\u(.{0,4})|\\(.?))/g; | ||
const UNICODE_PATTERN = /^[0-9a-fA-F]{4}$/; | ||
export function convertLine(line: string): string { | ||
return line.replace(CONVERT_RXP, (_, unicode, char) => { | ||
const ENCODE_PATTERN = /(?:[\u0000-\u001F\\\u007F-\uFFFF])/g; | ||
const ENCODE_KEY_PATTERN = /(?:[\u0000-\u0020!#:=\\\u007F-\uFFFF])/g; // ENCODE_PATTERN with separators + comments | ||
export function decodeLine(line: string): string { | ||
return line.replace(DECODE_PATTERN, (_, unicode, char) => { | ||
if (unicode !== undefined) { | ||
if (!unicode.match(UNICODE_RXP)) { | ||
if (!unicode.match(UNICODE_PATTERN)) { | ||
throw new Error('Malformed \\uxxxx encoding.'); | ||
@@ -26,2 +29,36 @@ } | ||
export function encodeLine(line: string, isKey?: boolean): string { | ||
let str = line.replace(isKey ? ENCODE_KEY_PATTERN : ENCODE_PATTERN, (c) => { | ||
if (c === '\t') { | ||
return '\\t'; | ||
} else if (c === '\r') { | ||
return '\\r'; | ||
} else if (c === '\n') { | ||
return '\\n'; | ||
} else if (c === '\f') { | ||
return '\\f'; | ||
} else if (c >= ' ' && c <= '~') { | ||
return '\\' + c; | ||
} else { | ||
const code = c.charCodeAt(0); | ||
if (code < 16) return '\\u000' + code.toString(16).toUpperCase(); | ||
if (code < 256) return '\\u00' + code.toString(16).toUpperCase(); | ||
if (code < 4096) return '\\u0' + code.toString(16).toUpperCase(); | ||
return '\\u' + code.toString(16).toUpperCase(); | ||
} | ||
}); | ||
if (!isKey) { | ||
const c = str.charAt(0); | ||
if (c === ' ' || c === '\t' || c === '\f') { | ||
str = '\\' + str; | ||
} | ||
} | ||
return str; | ||
} | ||
/** | ||
* @deprecated Use {@link #decodeLine}. | ||
*/ | ||
export const convertLine = decodeLine; | ||
export class LineReader { | ||
@@ -28,0 +65,0 @@ private readonly str: string; |
Sorry, the diff of this file is not supported yet
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
42429
873
166