Giter Site home page Giter Site logo

properties's Introduction

Properties-Reader

An ini file compatible properties reader for Node.JS

Installation

The easiest installation is through NPM:

npm install properties-reader

API

Read properties from a file:

var propertiesReader = require('properties-reader');
var properties = propertiesReader('/path/to/properties.file');

The properties are then accessible either by fully qualified name, or if the property names are in dot-delimited notation, they can be access as an object:

// fully qualified name
var property = properties.get('some.property.name');

// by object path
var property = properties.path().some.property.name;

To read more than one file, chain calls to the .append() method:

properties.append('/another.file').append('/yet/another.file');

To read properties from a string, use the .read() method:

properties.read('some.property = Value \n another.property = Another Value');

To set a single property into the properties object, use .set():

properties.set('property.name', 'Property Value');

When reading a .ini file, sections are created by having a line that contains just a section name in square brackets. The section name is then prefixed to all property names that follow it until another section name is found to replace the current section.

# contents of properties file
[main]
some.thing = foo

[blah]
some.thing = bar

// reading these back from the properties reader
properties.get('main.some.thing') == 'foo';
properties.get('blah.some.thing') == 'bar';

// looping through the properties reader
properties.each((key, value) => {
  // called for each item in the reader,
  // first with key=main.some.thing, value=foo
  // next with key=blah.some.thing, value=bar
});

// get all properties at once
expect(properties.getAllProperties()).toEqual({
  'main.some.thing': 'foo',
  'blah.some.thing': 'bar',
})

Checking for the current number of properties that have been read into the reader:

var propertiesCount = properties.length;

The length is calculated on request, so if accessing this in a loop an efficiency would be achieved by caching the value.

When duplicate names are found in the properties, the first one read will be replaced with the later one.

To get the complete set of properties, either loop through them with the .each((key, value) => {}) iterator or use the convenience method getAllProperties to return the complete set of flattened properties.

Saving changes

Once a file has been read and changes made, saving those changes to another file is as simple as running:

// async/await ES6
const propertiesReader = require('properties-reader');
const props = propertiesReader(filePath, {writer: { saveSections: true }});
await props.save(filePath);

// ES5 callback styles
props.save(filePath, function then(err, data) { ... });

// ES5 promise style
props.save(filePath).then(onSaved, onSaveError);

To output the properties without any section headings, set the saveSections option to false

Data Types

Properties will automatically be converted to their regular data types when they represent true/false or numeric values. To get the original value without any parsing / type coercion applied, use properties.getRaw('path.to.prop').

FAQ / Breaking Changes

Duplicate Section Headings

From version 2.0.0 the default behaviour relating to multiple [section] blocks with the same name has changed so combine the items of each same-named section into the one section. This is only visible when saving the items (via reader.save()).

To restore the previous behaviour which would allow duplicate [...] blocks to be created, supply an appender configuration with the property allowDuplicateSections set to true.

const propertiesReader = require('properties-reader');
const props = propertiesReader(filePath, 'utf-8', { allowDuplicateSections: true });

Contributions

If you find bugs or want to change functionality, feel free to fork and pull request.

properties's People

Contributors

auridevil avatar calebboyd avatar dependabot[bot] avatar droobertzka avatar era5mx avatar github-actions[bot] avatar steveukx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

properties's Issues

Cannot read URLs

Define URL in properties:
[trade]
base.api.url=http://google.com

Call:
const baseApiUrl = generalProperties.read('trade.base.api.url');

Result:
"[object Object]/public/getmarkets"

It's ok if you change the value to something plain

How I can save new property value after set it?

  1. When reading a .ini file, I have tried to set new value for exist property
  2. After that I have tried to save props but It seems can't save new value? How can I deal with it?

This is my code:

const PropertiesReader = require('properties-reader');
const filePath = './docs/XBStation.ini';
const props = PropertiesReader(filePath);
props.set('General.Username', 'Nothing on You');
props.save('./docs/_XBStation.ini', (err, data) => { console.log(data); });

Should provide a way to force values as strings.

Autodetecting the type of the properties can be problematic for String values written with digits only and starting with "0".

Example :

terminal.identifier=0111

This will get the number "111" when we want the string "0111"

Snyk - potential prototype pollution

Steps to reproduce:

payload.properties

[__proto__]
polluted = polluted

poc.js:

var propertiesReader = require('properties-reader');

propertiesReader('./payload.properties');

console.log({}.polluted) // logs 'polluted'

Convert ini file with dot-delimited notation into one big object

Hi I want to covert my ini file which looks like :

info.info1=ans1.
info.info2=ans2.
info3.info4=ans3.
info5.info6=ans4.

to an object:

{ info:{ info1: ans1, info2:ans2 }, info3:{ info4:ans3 }, info5{ info6: ans4 } }

in the version 2.1.1 this was working by

const propertiesReader = require('properties-reader'); const properties = propertiesReader("relative path"); const obj = properties.path();

but now when I updated to the latest version 2.2.0 path is returning an empty object

in the latest version

const obj = properties.getAllProperties();
is working but i want a nested object

Allow reading the properties from a URL

The current implementation, using the fs module, allows the properties file to be read only from the local file system. A use case I came across needed to access the properties file across a network, via http/https. Could this be added easily enough, or is it too much work or not appropriate?

Allow to write without sections

Hi Guys,

my project is using the properties files for translations, but without sections. It would be great, if I could dissable the sections on write in a way.

I provided #24 for this - please review and merge at your convenience, if you deem this usefull.

Thx a lot!
Andreas

Does not consider the case of properties whose parents are also properties

I don't think my title was very good but this is what I mean.

Consider this property file:

app=My App
app.version=2

First this library will create a property called this._propertiesExpanded.app and set it equal to 'My App'. Then it will try to create this._propertiesExpanded.app.version. This will fail because this._propertiesExpanded.app is a string, not an object and it can't have properties.

I could fix it, at least for my own purposes, but I'm a bit confused by the line in the set method:
source = (source[step] = source[step] || {});

What exactly does that do? It seems to be doing a few things at once on that line.

import syntax not working

Hello,

I am not sure if I am mistaken, but I have no import-syntax working for properties-reader (using TypeScript).
Documentation:
const propertiesReader = require("properties-reader"); // works fine
const properties = propertiesReader("path");

Trying to use modern import-syntax does not work for me.
import * as propertiesReader from "properties-reader"); // does not work
import {PropertiesReader} from "properties-reader"; // does not work
import {propertiesReader} from "properties-reader"; // does not work
import {PropertiesReader} from "properties-reader/src/properties-reader"; // does not work
[...]

This might not be an issue but just me not understanding why this is not working. I have looked up require to import converters, which would convert the require-statement into one of the import statements above.
I think it is not working because of the unusual directory-structure of npm-properties-reader. Usually an npm module has an index.js file directly in the directory folder exporting all other visible modules.

I think it would be great if properties-reader import-syntax would be fair to conventions.

initialize from JSON string

May be an enhancement. Initialize from JSON string to facilitate the properties sharing in Node.js worker threads.

const propsReader = require('properties-reader');
//Now
global.env = propsReader(path/to/file/name);
//Future
global.env = propsReader(path/to/file/name, JSON string, writeToFile); //Write to file, if true

For architectural reasons, don't allow threads to read the properties from file. You might know that worker_threads doesn't allow object sharing

Not exactly an issue, but some further explainations maybe ?

From the main explanation, it says that properties contents can be displayed either by 'getAllProperties' or by looping through them with the .each((key, value) => {}) iterator. Sorry for my weak 'culture', but it would be nice if you can provide an example in this the case either with the iterator. Thanks in advance, good job!

How to read other format nls files?

How to read other format nls files? Like test.js, in the test.js, there are some key and value like below format:
({
key1: "value1",
key2: "value2",
})
Thanks.

Is it possible to have = signs in the keys?

For example I need a property that would be like:
/Application[@name='TestApp']/Attributes/Map/entry[@key='file']/@value=file.txt

Now the reader will stop reading the first key at the ='TestApp'.
Is it possible to escape the = signs so that it reads till the end of the token?

Can't disable creation of sections

The initialization with the parameter 'saveSections: false' is not working. I have to enter property-writer.js and set manually to false.

properties.path() not working for dot-separated keys after upgrade from 2.1.1 to 2.2.0

Hi, thanks for this library! I'm using it to parse Spring-like properties files.

Given a properties file with no header and a dot-separated value:

testkey.nested=testval

In 2.1.1 I used to be able to do:

const propertiesReader = require("properties-reader");
const properties = propertiesReader("test.properties");
properties.path()

This would give an object: { testkey: { nested: 'testval' } }

In 2.2.0, path() here returns: {}

Adding a property to a pre-existing section results in a duplicated section

Given a file with contents

[section1]
s1foo=foo
s1bar=bar
[section2]
s2foo=foo
s2bar=bar
[section3]
s3foo=foo
s3bar=bar

the following code

const PropertiesReader = require('properties-reader');
let properties = PropertiesReader('/path/to/input/file');
properties.set(`section2.s2new`, 'new');
await properties.save('/path/to/output/file');

will result in

[section1]
s1foo=foo
s1bar=bar
[section2]
s2foo=foo
s2bar=bar
[section3]
s3foo=foo
s3bar=bar
[section2]
s2new=new

The use case here is of course modification of an existing ini file. IMO, the expected output would be the addition of the new section property in the pre-existing section, such as

[section1]
s1foo=foo
s1bar=bar
[section2]
s2foo=foo
s2bar=bar
s2new=new
[section3]
s3foo=foo
s3bar=bar

instead of duplicating the section at the bottom with the new property. In this manner, it seems the library does not properly support the addition of new properties in pre-existing sections that are not last in the file, only modification of such section's pre-existing properties. The output above is invalid as many parsers will throw a duplicate section error.

Parametric properties file

I wonder if it's possible to have a parametric property into a file.
Let me explain. Suppose into a file I have something like:

[main]
str1 = "stevekx says " + $param1 + "!"

It would be nice, if I could get the str1 value, passing the value of $ param1.
Something like:

var a = properties.get('main.str1', 'hello');
console.log(a) --> stevekx says hello!

Multiline support

Thanks for the great library!
Since my VSCode extension depends on it and I have some users requesting a multi-line functionality like

%%SOME_TAG%%=<List>\
<String>server1</String>\
<String>server2</String>\
<String>server3</String>\
</List>

Would it be possible to put a request for this feature?

Parsing is substantially slower than I'd expect

When presented with properties files that contain thousands of entries, this package can take multiple seconds to parse them. This is substantially slower than a naive line-by-line parse method, and slow enough to make this package infeasible for my use-case.

See this benchmark, which compares properties-reader to a naive solution using string splitting. Here's the output I get:

.read() x 23.15 ops/sec ±1.05% (42 runs sampled)
naive x 2,600 ops/sec ±0.89% (98 runs sampled)
Fastest is naive

I might expect a naive solution to be a bit faster because it needs fewer checks for complex cases like comments and headings, but being two orders of magnitude faster is very surprising.

can not read from another path

Hi,

I'm having an issue when reading a properties file from another path relative to the caller on a Windows machine. Or in other words: I can only use the same path where the script is, which uses the properties file.

//works:
var properties = PropertiesReader('gradle.properties');

//the following combinations don't work:
var properties = PropertiesReader('./props/gradle.properties');
var properties = PropertiesReader('props\gradle.properties');
var properties = PropertiesReader('.\props\gradle.properties');
var properties = PropertiesReader('../gradle.properties');

My machine runs Windows 7 with NodeJs in version 2.14.7 and properties-reader 0.0.15

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.