0.2.1 • Published 4 years ago

use-properties v0.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

use-properties

use-properties allows you to centralized text management and replacement

Usage

Installation

Use npm to install (or yarn if you'd like).

npm install use-properties

Then import the class Properties from use-properties in your script.

import Properties from 'use-properties'       // ESM
const Properties = require('use-properties')  // CJS

Preparing

Put the properties file in your project.

# hoge.properties

m1=Hello
m2=Goodbye, {0}!
m3={0} is {1} years old.

Then you can create the Properties instance.

// hoge.js

import Properties from 'use-properties'

const appText = new Properties('./path/to/properties')

Getting properties

// hoge.js
...

console.log(appText.m1.get()) 
// Hello

console.log(appText.m2.get())
// Goodbye, {0}!

console.log(appText.m2.get('Alice'))
// Goodbye, Alice!

console.log(appText.m3.get('Alice'))
// Alice is {1} years old.

console.log(appText.m3.get('Alice', '20'))
// Alice is 20 years old.

Overall

# path: ${PROJECT_ROOT}/test/hoge.properties

m1=Hello
m2=Goodbye, {0}!
m3={0} is {1} years old.
// path: ${PROJECT_ROOT}/test/hoge.js

import Properties from 'use-properties'
const appText = new Properties('test/hoge.properties')


console.log(appText.m1.get()) 
console.log(appText.m2.get())
console.log(appText.m2.get('Alice'))
console.log(appText.m3.get('Alice'))
console.log(appText.m3.get('Alice', '20'))
// Hello
// Goodbye, {0}!
// Goodbye, Alice!
// Alice is {1} years old.
// Alice is 20 years old.

FAQ

How to line feed in properties file?

To line feed in a property, type \n.
And to avoid line feed, type \\n.

examples:

m4=lorem\nipsum dolor sit amet
m5=You can use `\\n` to line feed.
console.log(appText.m4.get())
/*
lorem
ipsum dolor sit amet
*/

console.log(appText.m5.get())
// You can use `\n` to line feed.

Can I use = in a property?

Yes, of course!
In the Properties class, properties are split by rows.
Then the key and value of property are split by the first =.

examples:

m6=lorem=ipsum=dolor=sit=amet
console.log(appText.m6.get())
// lorem=ipsum=dolor=sit=amet

How to get the value if I named key which starts with number?

Use the bracket notation.

examples:

001=asdf
console.log(appText['001'].get())
// asdf
0.2.1

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago

0.0.1

4 years ago