2.0.0 • Published 4 years ago

properties-like v2.0.0

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

properties-like

npm package

NPM version NPM Downloads npm.io

This module implements the Java .properties specification


Installation

CommonJS style with npm

npm install --save properties-like

In node env:

const Properties = require('properties-like');
const props = new Properties(); // Properties.getProperties()
props.loadFrom('./jdbc.properties');
// or
props.load(fs.readFileSync('./jdbc.properties'));

In webpack env:

{
  module: {
    rules: [{
      test: /\.properties$/,
      loader: 'properties-like/webpack-loader'
    }]
  }
}

Load properties-like via classical <script> tag

<script src="https://cdn.jsdelivr.net/npm/properties-like/browser/umd.min.js"></script>
<script>
  var props = new Properties(); // Properties.getProperties()
  props.load('key=value\nkey2=value2');
  props.getProperty('key'); // ==> 'value'
  props.getProperty('key2'); // ==> 'value2'
</script>
<script src="https://cdn.jsdelivr.net/npm/properties-like/browser/parse.umd.min.js"></script>
<script>
  var props = {};
  parseProperties('key=value\nkey2=value2', function(key, value) {
    props[key] = value;
  });
  console.log(props); // ==> { key: 'value', key2: 'value2' }
</script>

API

Instance Method Summary

实例方法

Modifier and TypeInstance MethodsDescription
thisclear()Clears this property list so that it contains no keys.清空属性集合对象内的key和value。
Propertiesclone()Creates a shallow copy of this property list.浅克隆一个属性集合对象。
BooleancontainsKey​(key)key: StringTests if the specified object is a key in this property list.检测是否包含指定key。
BooleancontainsValue​(value)value: anyReturns true if this property list maps one or more keys to this value.检测是否包含指定value。
thisforEach(callback)callback: FunctionPerforms the given action for each entry in this map until all entries have been processed or the action throws an exception.遍历属性集合,在回调函数中返回包含value和key的数组。
anyget​(key [, defaultValue])getProperty(key [, defaultValue])key: StringdefaultValue: any(optional)Returns the value to which the specified key is mapped, or undefined if this map contains no mapping for the key.根据指定的key返回对应的value,如果未匹配到就返回默认值或者undefined。
NumbergetNumber(key [, defaultValue])key: StringdefaultValue: any(optional)Returns the value to which the specified key is mapped, and convert it to Number.根据指定的key返回对应的value,并转成数字。
NumbergetBoolean(key [, defaultValue])key: StringdefaultValue: any(optional)Returns the value to which the specified key is mapped, and convert it to Boolean.根据指定的key返回对应的value,并转成bool值。
BooleanisEmpty​()Tests if this property list maps no keys to values.判断是否是空集合。
Array\keys​()propertyNames()Returns an array of the keys in this property list.返回所有的key
thisload​(content)content: Stringtransform: FunctionReads a property list (key and element pairs) from the content.从字符串中提取出key和value。
thisloadFrom(filePath)filePath: Stringtransform: FunctionLoads all of the properties from a file.从文件中提取出key和value。
thisput​(key, value)set​(key, value)setProperty​(key, value)key: Stringvalue: anyMaps the specified key to the specified value in this property list.暂存key和value至属性集合。
NumberputAll​(t)setProperties​(t)t: Object | ArrayCopies all of the mappings from the specified map to this property list.把数组或者对象浅拷贝至属性集合。
Booleanremove​(key [, specifiedValue])delete​(key [, specifiedValue])key: String specifiedValue: any(optional)Removes the entry for the specified key or even it is mapped to the specified value.移除指定的key的映射或者也包含指定的value才被移除。
Booleanreplace​(key, newValue)key: String newValue: anyReplaces the entry for the specified key only if it is currently mapped to some value.替换已存在的指定的key的映射。
Numbersize​()Returns the number of keys in this property list.属性集合内key的数量
thisstore​(filePath [, fsOptions])filePath: String fsOptions: ObjectWrites this property list (key and element pairs) in this Properties table to a file.把key&value写入文件
Arrayvalues​()Returns a Array of the values contained in this map.返回所有的value

Static Method Summary

静态方法

Modifier and TypeStatic MethodDescription
voidparse(inCharBuf, callback) inCharBuf: String | Arraycallback: FunctionTry to parse out key and element pairs from a given String/Array尝试从字符串中解析出key和value

Usage

Create a test.properties file

# backslash
jdbc.mysql.driver     = com.mysql.jdbc.Driver
jdbc.mysql.url        = jdbc\:mysql\://localhost\:3306/test?useUnicode\=true&characterEncoding\=gbk
jdbc .mysql\ .username= test\
123456789
jdbc\:mysql.password   = ~\ !@#$%^&*
jdbc.mysql\=maxActive  = \ \ 50\ 

# Unicode
username=\u7528 \u6237 \u540d
\u8d26 \u53f7=test123
\u6635 \u79f0=\u540a \u70b8 \u5929
p

int=1
bool=true

Read and parse the file

const Properties = require('properties-like');
const props = new Properties();
props.loadFrom('./test.properties');

props.get('jdbc.mysql.driver'); // ==> 'com.mysql.jdbc.Driver'
props.getProperty('jdbc.mysql.driver     '); // ==> undefined
props.getProperty('jdbc.mysql.url'); // ==> 'jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk'
props.getProperty('jdbc.mysql .username'); // ==> 'test123456789'
props.getProperty('jdbc:mysql.password'); // ==> '~ !@#$%^&*'
props.getProperty('jdbc.mysql=maxActive'); // ==> '  50 '
props.getProperty('username'); // ==> '用户名'
props.getProperty('账号'); // ==> 'test123'
props.getProperty('昵称'); // ==> '吊炸天'
props.getProperty('p'); // ==> ''
props.getNumber('int'); // ==> 1
props.getBoolean('bool'); // ==> true

props.forEach((value, key) => {

});

for (let [value, key, index] of props) {

}

Browser Support

ChromeFirefoxSafariOperaEdgeIE
Latest ✔Latest ✔Latest ✔Latest ✔Latest ✔9 ✔
2.0.0

4 years ago

1.0.4

4 years ago

2.0.0-1

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

1.0.0-beta.3

5 years ago

1.0.0-beta.1

5 years ago