properties-like
.properties specification
This module implements the Java
Installation
CommonJS style with npm
npm install --save properties-like
In node env:
const Properties = ;const props = ; // Properties.getProperties()props;// orprops;
In webpack env:
module: rules: test: /\.properties$/ loader: 'properties-like/webpack-loader'
properties-like
via classical <script>
tag
Load
API
Instance Method Summary
实例方法
Modifier and Type | Instance Methods | Description |
---|---|---|
this | clear() |
Clears this property list so that it contains no keys. 清空属性集合对象内的key和value。 |
Properties | clone() |
Creates a shallow copy of this property list. 浅克隆一个属性集合对象。 |
Boolean | containsKey(key) key: String |
Tests if the specified object is a key in this property list. 检测是否包含指定key。 |
Boolean | containsValue(value) value: any |
Returns true if this property list maps one or more keys to this value. 检测是否包含指定value。 |
this | forEach(callback) callback: Function |
Performs the given action for each entry in this map until all entries have been processed or the action throws an exception. 遍历属性集合,在回调函数中返回包含value和key的数组。 |
any | get(key [, defaultValue]) getProperty(key [, defaultValue]) key: String defaultValue: 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。 |
Number | getNumber(key [, defaultValue]) key: String defaultValue: any(optional) |
Returns the value to which the specified key is mapped, and convert it to Number. 根据指定的key返回对应的value,并转成数字。 |
Number | getBoolean(key [, defaultValue]) key: String defaultValue: any(optional) |
Returns the value to which the specified key is mapped, and convert it to Boolean. 根据指定的key返回对应的value,并转成bool值。 |
Boolean | isEmpty() |
Tests if this property list maps no keys to values. 判断是否是空集合。 |
Array<String> | keys() propertyNames() |
Returns an array of the keys in this property list. 返回所有的key |
this | load(content) content: String transform: Function |
Reads a property list (key and element pairs) from the content. 从字符串中提取出key和value。 |
this | loadFrom(filePath) filePath: String transform: Function |
Loads all of the properties from a file. 从文件中提取出key和value。 |
this | put(key, value) set(key, value) setProperty(key, value) key: String value: any |
Maps the specified key to the specified value in this property list. 暂存key和value至属性集合。 |
Number | putAll(t) setProperties(t) t: Object | Array |
Copies all of the mappings from the specified map to this property list. 把数组或者对象浅拷贝至属性集合。 |
Boolean | remove(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才被移除。 |
Boolean | replace(key, newValue) key: String newValue: any |
Replaces the entry for the specified key only if it is currently mapped to some value. 替换已存在的指定的key的映射。 |
Number | size() |
Returns the number of keys in this property list. 属性集合内key的数量 |
this | store(filePath [, fsOptions]) filePath: String fsOptions: Object |
Writes this property list (key and element pairs) in this Properties table to a file. 把key&value写入文件 |
Array | values() |
Returns a Array of the values contained in this map. 返回所有的value |
Static Method Summary
静态方法
Modifier and Type | Static Method | Description |
---|---|---|
void | parse(inCharBuf, callback) inCharBuf: String | Array callback: Function |
Try 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 = ;const props = ;props; props; // ==> 'com.mysql.jdbc.Driver'props; // ==> undefinedprops; // ==> 'jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk'props; // ==> 'test123456789'props; // ==> '~ !@#$%^&*'props; // ==> ' 50 'props; // ==> '用户名'props; // ==> 'test123'props; // ==> '吊炸天'props; // ==> ''props; // ==> 1props; // ==> true props; for let value key index of props
Browser Support
Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 9 ✔ |