Comparing version 1.0.2 to 1.0.3
@@ -7,4 +7,2 @@ /// <reference types="node" /> | ||
private immutable; | ||
private parseObject; | ||
private toObject; | ||
get<T = any>(key: string, defaultValue?: T): T; | ||
@@ -23,2 +21,3 @@ set(key: string, value: any): this; | ||
private static parse; | ||
private toObject; | ||
} |
@@ -8,22 +8,3 @@ import { readFileSync, promises, writeFileSync } from 'fs'; | ||
this.immutable = false; | ||
for (const key in config) { | ||
config[key] = this.parseObject(config[key]); | ||
} | ||
} | ||
parseObject(value) { | ||
if (typeof value == "object" && !Array.isArray(value) && !(value instanceof Config)) { | ||
const cfg = new Config(value); | ||
value = cfg; | ||
} | ||
return value; | ||
} | ||
toObject() { | ||
const config = this.config; | ||
const res = {}; | ||
for (const key in config) { | ||
const value = config[key]; | ||
res[key] = (value instanceof Config) ? value.toObject() : value; | ||
} | ||
return res; | ||
} | ||
get(key, defaultValue) { | ||
@@ -34,7 +15,12 @@ if (this.config.hasOwnProperty(key)) | ||
const tree = key.split("."); | ||
const l = tree.length; | ||
let newKey = tree[0]; | ||
const value = this.config[newKey]; | ||
if (!(value instanceof Config)) | ||
throw new ReferenceError(""); | ||
return value.get(tree.slice(1).join("."), defaultValue); | ||
let i = 1; | ||
for (; !this.config.hasOwnProperty(newKey) && i < l; i++) | ||
newKey += "." + tree[i]; | ||
if (i != l) { | ||
const value = this.config[newKey]; | ||
if (typeof value == "object" && !Array.isArray(value)) | ||
return new Config(value).get(tree.slice(i).join("."), defaultValue); | ||
} | ||
} | ||
@@ -48,15 +34,22 @@ if (defaultValue === undefined) | ||
throw new ReferenceError("Config immutable"); | ||
if (typeof value == "object" && !Array.isArray(value) && !(value instanceof Config)) { | ||
const cfg = new Config(value); | ||
value = cfg; | ||
if (this.config.hasOwnProperty(key)) { | ||
this.config[key] = value; | ||
} | ||
if (key.indexOf(".") != -1) { | ||
else if (key.indexOf(".") != -1) { | ||
const tree = key.split("."); | ||
const l = tree.length; | ||
let newKey = tree[0]; | ||
let cfg = this.config[newKey]; | ||
if (cfg === undefined) | ||
cfg = this.config[newKey] = new Config(); | ||
if (!(cfg instanceof Config)) | ||
throw new ReferenceError(""); | ||
cfg.set(tree.slice(1).join("."), value); | ||
let i = 1; | ||
for (; !this.config.hasOwnProperty(newKey) && i < l; i++) | ||
newKey += "." + tree[i]; | ||
if (i != l) { | ||
const _value = this.config[newKey]; | ||
if (typeof _value == "object" && !Array.isArray(_value)) { | ||
const cfg = new Config(_value).set(tree.slice(i).join("."), value); | ||
this.config[newKey] = cfg.toObject(); | ||
} | ||
else { | ||
this.config[newKey] = value; | ||
} | ||
} | ||
} | ||
@@ -112,3 +105,3 @@ else { | ||
const strategy = (strategy => new strategy())(Strategy.getStrategy(path)); | ||
this.config = this.parseObject(strategy.parse(raw)); | ||
this.config = strategy.parse(raw); | ||
} | ||
@@ -130,2 +123,5 @@ static async parseFromFile(path, immutable = false) { | ||
} | ||
toObject() { | ||
return Object.assign({}, this.config); | ||
} | ||
} |
/// <reference types="node" /> | ||
import Strategy from "./Strategy.js"; | ||
declare class JSONStrategy extends Strategy { | ||
protected parseObj(obj: Object, path?: string[]): Object; | ||
parse(raw: string | Buffer): Object; | ||
@@ -6,0 +5,0 @@ stringify(raw: Object): string; |
import Strategy from "./Strategy.js"; | ||
class JSONStrategy extends Strategy { | ||
parseObj(obj, path = []) { | ||
const res = {}; | ||
for (const key in obj) { | ||
const value = obj[key]; | ||
if (key.indexOf(".") != -1) { | ||
const _path = [...path]; | ||
const tree = key.split("."); | ||
const l = tree.length - 1; | ||
let _obj = res; | ||
for (let i = 0; i < l; i++) { | ||
const subKey = tree[i]; | ||
_path.push(subKey); | ||
let subObj = _obj[subKey]; | ||
if (!subObj) | ||
subObj = _obj[subKey] = {}; | ||
if (typeof subObj != "object" || Array.isArray(subObj)) | ||
throw new ReferenceError(`The path "${_path.join(".")}" already exists`); | ||
_obj = _obj[subKey]; | ||
} | ||
_path.push(tree[l]); | ||
if (typeof value == "object" && !Array.isArray(value)) | ||
_obj[tree[l]] = this.parseObj(value, _path); | ||
else | ||
_obj[tree[l]] = value; | ||
} | ||
else { | ||
const _path = [...path, key]; | ||
if (typeof value == "object" && !Array.isArray(value)) | ||
res[key] = this.parseObj(value, _path); | ||
else | ||
res[key] = value; | ||
} | ||
} | ||
return res; | ||
} | ||
parse(raw) { | ||
let obj = JSON.parse(raw.toString()); | ||
if (typeof obj == "object" && !Array.isArray(obj)) | ||
obj = this.parseObj(obj); | ||
return obj; | ||
return JSON.parse(raw.toString()); | ||
} | ||
@@ -44,0 +6,0 @@ stringify(raw) { |
/// <reference types="node" /> | ||
import JSONStrategy from "./JSONStrategy.js"; | ||
declare class YAMLStrategy extends JSONStrategy { | ||
import Strategy from "./Strategy.js"; | ||
declare class YAMLStrategy extends Strategy { | ||
parse(raw: string | Buffer): Object; | ||
@@ -5,0 +5,0 @@ stringify(raw: Object): string; |
import YAML from "yaml"; | ||
import JSONStrategy from "./JSONStrategy.js"; | ||
class YAMLStrategy extends JSONStrategy { | ||
import Strategy from "./Strategy.js"; | ||
class YAMLStrategy extends Strategy { | ||
parse(raw) { | ||
let obj = YAML.parse(raw.toString()); | ||
if (typeof obj == "object" && !Array.isArray(obj)) | ||
obj = this.parseObj(obj); | ||
return obj; | ||
return YAML.parse(raw.toString()); | ||
} | ||
@@ -10,0 +7,0 @@ stringify(raw) { |
{ | ||
"name": "simcfg", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A simple module for managing configuration files", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -68,2 +68,21 @@ # Simple Config(simcfg) | ||
#### !!!WARNING!!! | ||
With the following config entry: | ||
```json | ||
{ | ||
"a": 1, | ||
"a.b": 2 | ||
} | ||
``` | ||
```yml | ||
a: 1 | ||
a.b: 2 | ||
``` | ||
it is impossible to get the *b* field from *a* as from an object. | ||
```ts | ||
config.get("a.b"); //return 2 | ||
config.get("a"); //return 1 | ||
config.get("a").b // 1.b === undefined | ||
``` | ||
### Changing values | ||
@@ -70,0 +89,0 @@ If the config is immutable, the method will throw an error. |
@@ -36,15 +36,2 @@ # Simple Config(simcfg) | ||
**!!!ВАЖНО!!!** | ||
Следующая запись в конфиге бросит ошибку при чтении файла, из-за попытки перезаписи значения *a*. | ||
```json | ||
{ | ||
"a":1, | ||
"a.b":2 | ||
} | ||
``` | ||
```yml | ||
a: 1 | ||
a.b: 2 | ||
``` | ||
### Получение значений | ||
@@ -69,2 +56,21 @@ Если значение не найдено и стандартное значение не указано. метод бросит ошибку. | ||
#### !!!ВАЖНО!!! | ||
При следующей записи конфига: | ||
```json | ||
{ | ||
"a": 1, | ||
"a.b": 2 | ||
} | ||
``` | ||
```yml | ||
a: 1 | ||
a.b: 2 | ||
``` | ||
невозможно получить поле *b* из *a*, как из объекта. | ||
```ts | ||
config.get("a.b"); //return 2 | ||
config.get("a"); //return 1 | ||
config.get("a").b // 1.b === undefined | ||
``` | ||
### Изменение значений | ||
@@ -71,0 +77,0 @@ Если конфиг иммутабелен, метод бросит ошибку. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
112
14711
224