-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
96 lines (86 loc) · 1.75 KB
/
index.js
File metadata and controls
96 lines (86 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**!
* js-properties - index.js
*
* Copyright(c) luckydrq<drqzju@gmail.com>
*
* Authors:
* luckydrq <drqzju@gmail.com>
*/
'use strict';
/**
* Module dependencies.
*/
const toMap = require('object-to-map');
class Properties {
/**
* constructor
* @param {Map|Object} defaultProperties
* @api public
*/
constructor(defaultProperties) {
if (defaultProperties && !(defaultProperties instanceof Map)) {
defaultProperties = toMap(defaultProperties);
}
this.properties = defaultProperties || new Map();
}
/**
* #getProperty
* @param {String} key
* @param {String} defaultValue
* @return {String}
* @api public
*/
getProperty(key, defaultValue) {
let value = this.properties.get(key);
if (value == null) {
value = defaultValue;
}
return value;
}
/**
* #setProperty
* @param {String} key
* @param {String} value
* @return {String}
* @api public
*/
setProperty(key, value) {
this.properties.set(key, value);
return value;
}
/**
* #load
*
*/
load(str) {
str.trim()
.replace(/^\{/, '')
.replace(/\}$/, '')
.split(',')
.forEach(function(tuple) {
let pair = tuple.split('=');
let key = pair[0].trim();
let value = pair[1].trim();
this.setProperty(key, value);
}, this);
return this;
}
/**
* #toString
* @return {String}
* @api public
*/
toString() {
let properties = this.properties;
let arr = [];
for (let key of properties.keys()) {
arr.push(serialize(key));
}
return '{' + arr.join(',') + '}';
function serialize(key) {
let value = properties.get(key);
return [key, value].join('=');
}
}
}
module.exports = Properties;