Installing dependencies.
This commit is contained in:
+92
@@ -0,0 +1,92 @@
|
||||
# java-properties
|
||||
|
||||

|
||||
|
||||
Read Java .properties files. Supports adding dynamically some files and array key value (same key multiple times)
|
||||
|
||||
## Getting Started
|
||||
|
||||
Install the module with: `npm install java-properties`
|
||||
|
||||
## Documentation
|
||||
|
||||
```javascript
|
||||
var properties = require('java-properties');
|
||||
|
||||
// Reference a properties file
|
||||
var values = properties.of('values.properties');
|
||||
|
||||
//Read a value from the properties file
|
||||
values.get('a.key'); //returns value of a.key
|
||||
|
||||
//Add an additional file's properties
|
||||
values.add('anotherfile.properties');
|
||||
|
||||
//Clear out all values
|
||||
values.reset();
|
||||
...
|
||||
// returns the value of a.key of 'defaultValue' if key is not found
|
||||
values.get('a.key', 'defaultValue');
|
||||
...
|
||||
// returns the value of the a.int.key as an int or 18
|
||||
values.getInt('a.int.key', 18);
|
||||
...
|
||||
// returns the value of the a.float.key as a float or 18.23
|
||||
values.getFloat('a.float.key', 18.23);
|
||||
...
|
||||
// returns the value of the a.bool.key as an boolean. Parse true or false with any case or 0 or 1
|
||||
values.getBoolean('a.bool.key', true);
|
||||
...
|
||||
// returns all the keys
|
||||
values.getKeys();
|
||||
...
|
||||
// adds another file the properties list
|
||||
values.addFile('anotherFile.properties');
|
||||
...
|
||||
// empty the keys previously loaded
|
||||
values.reset();
|
||||
...
|
||||
[ -- .properties file
|
||||
an.array.key=value1
|
||||
an.array.key=value2
|
||||
]
|
||||
values.get('an.array.key'); // returns [value1, value2]
|
||||
|
||||
// Multiple contexts
|
||||
var myFile = new PropertiesFile(
|
||||
'example.properties',
|
||||
'arrayExample.properties');
|
||||
myFile.get('arrayKey');
|
||||
|
||||
var myOtherFile = new PropertiesFile();
|
||||
myOtherFile.addFile('example.properties');
|
||||
myOtherFile.addFile('example2.properties');
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.
|
||||
|
||||
## Release History
|
||||
|
||||
- 0.1.0 Initial commit
|
||||
- 0.1.5 Support empty strings
|
||||
- 0.1.6 New API: `getKeys`
|
||||
- 0.1.7 New APIs: `addFile` and `reset`
|
||||
- 0.1.8 Add array key (the same key many time in files)
|
||||
- 0.2.0 Wrap features into a class to be able to have multiple running contexts
|
||||
- 0.2.1 Add default value to get method. Add getInt and getFloat to get an integer or float value
|
||||
- 0.2.2 Add getBoolean method to get a value as a boolean. Accepted values are true, TRUE, false, FALSE, 0, 1
|
||||
- 0.2.3 Add getMatchingKeys method
|
||||
- 0.2.4 Allow multi-line properties
|
||||
- 0.2.5 Refactorings, no new features
|
||||
- 0.2.6 FIX interpolation when a property is multivalued
|
||||
- 0.2.7 Get only last value for int and boolean in case of multivalued attribute
|
||||
- 0.2.8 FIX unicode \uxxxx char decoding
|
||||
- 0.2.9 Allow multiple double quotation marks
|
||||
- 0.2.10 fix bug with escaped : & = (thanks @Drapegnik)
|
||||
- 1.0.0 Rewrite as Typescript. Support Node 6+ only
|
||||
|
||||
## License
|
||||
|
||||
Licensed under the MIT license.
|
||||
+216
@@ -0,0 +1,216 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.of = exports.PropertiesFile = void 0;
|
||||
|
||||
var _fs = _interopRequireDefault(require("fs"));
|
||||
|
||||
function _interopRequireDefault(obj) {
|
||||
return obj && obj.__esModule ? obj : {
|
||||
default: obj
|
||||
};
|
||||
}
|
||||
/*
|
||||
* properties
|
||||
*
|
||||
* Copyright (c) 2013 Matt Steele
|
||||
* Licensed under the MIT license.
|
||||
*/
|
||||
|
||||
|
||||
class PropertiesFile {
|
||||
constructor(...args) {
|
||||
this.objs = {};
|
||||
|
||||
if (args.length) {
|
||||
this.of.apply(this, args);
|
||||
}
|
||||
}
|
||||
|
||||
makeKeys(line) {
|
||||
if (line && line.indexOf('#') !== 0) {
|
||||
//let splitIndex = line.indexOf('=');
|
||||
let separatorPositions = ['=', ':'].map(sep => {
|
||||
return line.indexOf(sep);
|
||||
}).filter(index => {
|
||||
return index > -1;
|
||||
});
|
||||
let splitIndex = Math.min(...separatorPositions);
|
||||
let key = line.substring(0, splitIndex).trim();
|
||||
let value = line.substring(splitIndex + 1).trim(); // if keys already exists ...
|
||||
|
||||
if (this.objs.hasOwnProperty(key)) {
|
||||
// if it is already an Array
|
||||
if (Array.isArray(this.objs[key])) {
|
||||
// just push the new value
|
||||
this.objs[key].push(value);
|
||||
} else {
|
||||
// transform the value into Array
|
||||
let oldValue = this.objs[key];
|
||||
this.objs[key] = [oldValue, value];
|
||||
}
|
||||
} else {
|
||||
// the key does not exists
|
||||
const escapedValue = value.replace(/"/g, '\\"') // escape "
|
||||
.replace(/\\:/g, ':') // remove \ before :
|
||||
.replace(/\\=/g, '='); // remove \ before =
|
||||
|
||||
this.objs[key] = unescape(JSON.parse('"' + escapedValue + '"'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addFile(file) {
|
||||
let data = _fs.default.readFileSync(file, 'utf-8');
|
||||
|
||||
let items = data.split(/\r?\n/);
|
||||
let me = this;
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
let line = items[i];
|
||||
|
||||
while (line.substring(line.length - 1) === '\\') {
|
||||
line = line.slice(0, -1);
|
||||
let nextLine = items[i + 1];
|
||||
line = line + nextLine.trim();
|
||||
i++;
|
||||
}
|
||||
|
||||
me.makeKeys(line);
|
||||
}
|
||||
}
|
||||
|
||||
of(...args) {
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
this.addFile(args[i]);
|
||||
}
|
||||
}
|
||||
|
||||
get(key, defaultValue) {
|
||||
if (this.objs.hasOwnProperty(key)) {
|
||||
if (Array.isArray(this.objs[key])) {
|
||||
let ret = [];
|
||||
|
||||
for (let i = 0; i < this.objs[key].length; i++) {
|
||||
ret[i] = this.interpolate(this.objs[key][i]);
|
||||
}
|
||||
|
||||
return ret;
|
||||
} else {
|
||||
return typeof this.objs[key] === 'undefined' ? '' : this.interpolate(this.objs[key]);
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
getLast(key, defaultValue) {
|
||||
if (this.objs.hasOwnProperty(key)) {
|
||||
if (Array.isArray(this.objs[key])) {
|
||||
var lg = this.objs[key].length;
|
||||
return this.interpolate(this.objs[key][lg - 1]);
|
||||
} else {
|
||||
return typeof this.objs[key] === 'undefined' ? '' : this.interpolate(this.objs[key]);
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
getFirst(key, defaultValue) {
|
||||
if (this.objs.hasOwnProperty(key)) {
|
||||
if (Array.isArray(this.objs[key])) {
|
||||
return this.interpolate(this.objs[key][0]);
|
||||
} else {
|
||||
return typeof this.objs[key] === 'undefined' ? '' : this.interpolate(this.objs[key]);
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
getInt(key, defaultIntValue) {
|
||||
let val = this.getLast(key);
|
||||
|
||||
if (!val) {
|
||||
return defaultIntValue;
|
||||
} else {
|
||||
return parseInt(val, 10);
|
||||
}
|
||||
}
|
||||
|
||||
getFloat(key, defaultFloatValue) {
|
||||
let val = this.getLast(key);
|
||||
|
||||
if (!val) {
|
||||
return defaultFloatValue;
|
||||
} else {
|
||||
return parseFloat(val);
|
||||
}
|
||||
}
|
||||
|
||||
getBoolean(key, defaultBooleanValue) {
|
||||
function parseBool(b) {
|
||||
return !/^(false|0)$/i.test(b) && !!b;
|
||||
}
|
||||
|
||||
let val = this.getLast(key);
|
||||
|
||||
if (!val) {
|
||||
return defaultBooleanValue || false;
|
||||
} else {
|
||||
return parseBool(val);
|
||||
}
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
this.objs[key] = value;
|
||||
}
|
||||
|
||||
interpolate(s) {
|
||||
let me = this;
|
||||
return s.replace(/\\\\/g, '\\').replace(/\$\{([A-Za-z0-9\.\-\_]*)\}/g, function (match) {
|
||||
return me.getLast(match.substring(2, match.length - 1));
|
||||
});
|
||||
}
|
||||
|
||||
getKeys() {
|
||||
let keys = [];
|
||||
|
||||
for (let key in this.objs) {
|
||||
keys.push(key);
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
getMatchingKeys(matchstr) {
|
||||
let keys = [];
|
||||
|
||||
for (let key in this.objs) {
|
||||
if (key.search(matchstr) !== -1) {
|
||||
keys.push(key);
|
||||
}
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.objs = {};
|
||||
}
|
||||
|
||||
} // Retain 'of' from v1 for backward compatibility
|
||||
|
||||
|
||||
exports.PropertiesFile = PropertiesFile;
|
||||
|
||||
let of = function of(...args) {
|
||||
let globalFile = new PropertiesFile();
|
||||
globalFile.of.apply(globalFile, args);
|
||||
return globalFile;
|
||||
};
|
||||
|
||||
exports.of = of;
|
||||
+211
@@ -0,0 +1,211 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.of = exports.PropertiesFile = void 0;
|
||||
|
||||
var _fs = _interopRequireDefault(require("fs"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
/*
|
||||
* properties
|
||||
*
|
||||
* Copyright (c) 2013 Matt Steele
|
||||
* Licensed under the MIT license.
|
||||
*/
|
||||
class PropertiesFile {
|
||||
constructor(...args) {
|
||||
this.objs = {};
|
||||
|
||||
if (args.length) {
|
||||
this.of.apply(this, args);
|
||||
}
|
||||
}
|
||||
|
||||
makeKeys(line) {
|
||||
if (line && line.indexOf('#') !== 0) {
|
||||
//let splitIndex = line.indexOf('=');
|
||||
let separatorPositions = ['=', ':'].map(sep => {
|
||||
return line.indexOf(sep);
|
||||
}).filter(index => {
|
||||
return index > -1;
|
||||
});
|
||||
let splitIndex = Math.min(...separatorPositions);
|
||||
let key = line.substring(0, splitIndex).trim();
|
||||
let value = line.substring(splitIndex + 1).trim(); // if keys already exists ...
|
||||
|
||||
if (this.objs.hasOwnProperty(key)) {
|
||||
// if it is already an Array
|
||||
if (Array.isArray(this.objs[key])) {
|
||||
// just push the new value
|
||||
this.objs[key].push(value);
|
||||
} else {
|
||||
// transform the value into Array
|
||||
let oldValue = this.objs[key];
|
||||
this.objs[key] = [oldValue, value];
|
||||
}
|
||||
} else {
|
||||
// the key does not exists
|
||||
const escapedValue = value.replace(/"/g, '\\"') // escape "
|
||||
.replace(/\\:/g, ':') // remove \ before :
|
||||
.replace(/\\=/g, '='); // remove \ before =
|
||||
|
||||
this.objs[key] = unescape(JSON.parse('"' + escapedValue + '"'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addFile(file) {
|
||||
let data = _fs.default.readFileSync(file, 'utf-8');
|
||||
|
||||
let items = data.split(/\r?\n/);
|
||||
let me = this;
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
let line = items[i];
|
||||
|
||||
while (line.substring(line.length - 1) === '\\') {
|
||||
line = line.slice(0, -1);
|
||||
let nextLine = items[i + 1];
|
||||
line = line + nextLine.trim();
|
||||
i++;
|
||||
}
|
||||
|
||||
me.makeKeys(line);
|
||||
}
|
||||
}
|
||||
|
||||
of(...args) {
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
this.addFile(args[i]);
|
||||
}
|
||||
}
|
||||
|
||||
get(key, defaultValue) {
|
||||
if (this.objs.hasOwnProperty(key)) {
|
||||
if (Array.isArray(this.objs[key])) {
|
||||
let ret = [];
|
||||
|
||||
for (let i = 0; i < this.objs[key].length; i++) {
|
||||
ret[i] = this.interpolate(this.objs[key][i]);
|
||||
}
|
||||
|
||||
return ret;
|
||||
} else {
|
||||
return typeof this.objs[key] === 'undefined' ? '' : this.interpolate(this.objs[key]);
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
getLast(key, defaultValue) {
|
||||
if (this.objs.hasOwnProperty(key)) {
|
||||
if (Array.isArray(this.objs[key])) {
|
||||
var lg = this.objs[key].length;
|
||||
return this.interpolate(this.objs[key][lg - 1]);
|
||||
} else {
|
||||
return typeof this.objs[key] === 'undefined' ? '' : this.interpolate(this.objs[key]);
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
getFirst(key, defaultValue) {
|
||||
if (this.objs.hasOwnProperty(key)) {
|
||||
if (Array.isArray(this.objs[key])) {
|
||||
return this.interpolate(this.objs[key][0]);
|
||||
} else {
|
||||
return typeof this.objs[key] === 'undefined' ? '' : this.interpolate(this.objs[key]);
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
getInt(key, defaultIntValue) {
|
||||
let val = this.getLast(key);
|
||||
|
||||
if (!val) {
|
||||
return defaultIntValue;
|
||||
} else {
|
||||
return parseInt(val, 10);
|
||||
}
|
||||
}
|
||||
|
||||
getFloat(key, defaultFloatValue) {
|
||||
let val = this.getLast(key);
|
||||
|
||||
if (!val) {
|
||||
return defaultFloatValue;
|
||||
} else {
|
||||
return parseFloat(val);
|
||||
}
|
||||
}
|
||||
|
||||
getBoolean(key, defaultBooleanValue) {
|
||||
function parseBool(b) {
|
||||
return !/^(false|0)$/i.test(b) && !!b;
|
||||
}
|
||||
|
||||
let val = this.getLast(key);
|
||||
|
||||
if (!val) {
|
||||
return defaultBooleanValue || false;
|
||||
} else {
|
||||
return parseBool(val);
|
||||
}
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
this.objs[key] = value;
|
||||
}
|
||||
|
||||
interpolate(s) {
|
||||
let me = this;
|
||||
return s.replace(/\\\\/g, '\\').replace(/\$\{([A-Za-z0-9\.\-\_]*)\}/g, function (match) {
|
||||
return me.getLast(match.substring(2, match.length - 1));
|
||||
});
|
||||
}
|
||||
|
||||
getKeys() {
|
||||
let keys = [];
|
||||
|
||||
for (let key in this.objs) {
|
||||
keys.push(key);
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
getMatchingKeys(matchstr) {
|
||||
let keys = [];
|
||||
|
||||
for (let key in this.objs) {
|
||||
if (key.search(matchstr) !== -1) {
|
||||
keys.push(key);
|
||||
}
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.objs = {};
|
||||
}
|
||||
|
||||
} // Retain 'of' from v1 for backward compatibility
|
||||
|
||||
|
||||
exports.PropertiesFile = PropertiesFile;
|
||||
|
||||
let of = function of(...args) {
|
||||
let globalFile = new PropertiesFile();
|
||||
globalFile.of.apply(globalFile, args);
|
||||
return globalFile;
|
||||
};
|
||||
|
||||
exports.of = of;
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
declare class PropertiesFile {
|
||||
objs: {
|
||||
[key: string]: any;
|
||||
};
|
||||
constructor(...args: string[]);
|
||||
makeKeys(line: string): void;
|
||||
addFile(file: string): void;
|
||||
of(...args: string[]): void;
|
||||
get(key: string, defaultValue?: string): string | string[] | undefined;
|
||||
getLast(key: string, defaultValue?: string): string | undefined;
|
||||
getFirst(key: string, defaultValue?: string): string | undefined;
|
||||
getInt(key: string, defaultIntValue?: number): number | undefined;
|
||||
getFloat(key: string, defaultFloatValue?: number): number | undefined;
|
||||
getBoolean(key: string, defaultBooleanValue?: boolean): boolean;
|
||||
set(key: string, value: string): void;
|
||||
interpolate(s: string): string;
|
||||
getKeys(): string[];
|
||||
getMatchingKeys(matchstr: string): string[];
|
||||
reset(): void;
|
||||
}
|
||||
declare let of: (...args: any[]) => PropertiesFile;
|
||||
export { PropertiesFile, of };
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
{
|
||||
"name": "java-properties",
|
||||
"description": "Reads and interpolates Java .properties files",
|
||||
"version": "1.0.2",
|
||||
"license": "MIT",
|
||||
"files": [
|
||||
"dist-*/",
|
||||
"bin/"
|
||||
],
|
||||
"esnext": "dist-src/index.js",
|
||||
"main": "dist-node/index.js",
|
||||
"types": "dist-types/index.d.ts",
|
||||
"pika": true,
|
||||
"sideEffects": false,
|
||||
"keywords": [
|
||||
"java",
|
||||
"properties"
|
||||
],
|
||||
"homepage": "http://github.com/mattdsteele/java-properties",
|
||||
"bugs": {
|
||||
"url": "https://github.com/mattdsteele/java-properties/issues"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/mattdsteele/java-properties.git"
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"@pika/pack": "^0.3.7",
|
||||
"@pika/plugin-build-node": "^0.4.0",
|
||||
"@pika/plugin-build-types": "^0.4.0",
|
||||
"@pika/plugin-standard-pkg": "^0.4.0",
|
||||
"@pika/plugin-ts-standard-pkg": "^0.4.0",
|
||||
"@types/chai": "^4.1.7",
|
||||
"@types/mocha": "^5.2.7",
|
||||
"@types/node": "^11.13.17",
|
||||
"chai": "^4.2.0",
|
||||
"mocha": "^6.1.4",
|
||||
"ts-node": "^8.3.0",
|
||||
"typescript": "^3.5.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.6.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user