@nodecraft/ini
Advanced tools
Comparing version 2.1.2 to 2.2.0
50
ini.js
@@ -7,3 +7,3 @@ 'use strict'; | ||
const encode = (obj, opt) => { | ||
const encode = (obj, options) => { | ||
const children = []; | ||
@@ -13,13 +13,13 @@ let out = ''; | ||
// opt.section is passed in recursively. If passed in on top-level, it'll affect both the top-level ini keys, and any children | ||
if(typeof opt === 'string'){ | ||
opt = { | ||
section: opt, | ||
if(typeof options === 'string'){ | ||
options = { | ||
section: options, | ||
whitespace: false, | ||
inlineArrays: false | ||
inlineArrays: false, | ||
}; | ||
}else{ | ||
opt = opt || Object.create(null); | ||
opt.whitespace = opt.whitespace === true; | ||
options = options || Object.create(null); | ||
options.whitespace = options.whitespace === true; | ||
} | ||
const separator = opt.whitespace ? ' = ' : '='; | ||
const separator = options.whitespace ? ' = ' : '='; | ||
@@ -29,7 +29,7 @@ for(const [key, val] of Object.entries(obj)){ | ||
for(const item of val){ | ||
if(opt.inlineArrays){ | ||
out += safe(key) + separator + safe(item) + eol; | ||
if(options.inlineArrays){ | ||
out += safe(key, null, options) + separator + safe(item, null, options) + eol; | ||
}else{ | ||
// real code | ||
out += safe(key + '[]') + separator + safe(item) + eol; | ||
out += safe(key + '[]', null, options) + separator + safe(item, null, options) + eol; | ||
} | ||
@@ -40,8 +40,8 @@ } | ||
}else{ | ||
out += safe(key) + separator + safe(val) + eol; | ||
out += safe(key, null, options) + separator + safe(val, key, options) + eol; | ||
} | ||
} | ||
if(opt.section && out.length > 0){ | ||
out = '[' + safe(opt.section) + ']' + eol + out; | ||
if(options.section && out.length > 0){ | ||
out = '[' + safe(options.section, null, options) + ']' + eol + out; | ||
} | ||
@@ -51,7 +51,8 @@ | ||
const parsedSection = dotSplit(key).join('\\.'); | ||
const section = (opt.section ? opt.section + '.' : '') + parsedSection; | ||
const section = (options.section ? options.section + '.' : '') + parsedSection; | ||
const child = encode(obj[key], { | ||
section: section, | ||
whitespace: opt.whitespace, | ||
inlineArrays: opt.inlineArrays | ||
whitespace: options.whitespace, | ||
inlineArrays: options.inlineArrays, | ||
forceStringifyKeys: options.forceStringifyKeys, | ||
}); | ||
@@ -73,4 +74,4 @@ if(out.length > 0 && child.length > 0){ | ||
const decode = (str, opt = {}) => { | ||
const defaultValue = typeof opt.defaultValue !== 'undefined' ? opt.defaultValue : ''; | ||
const decode = (str, options = {}) => { | ||
const defaultValue = typeof options.defaultValue !== 'undefined' ? options.defaultValue : ''; | ||
@@ -121,3 +122,3 @@ const out = Object.create(null); | ||
} | ||
}else if(opt.inlineArrays && typeof(ref[key]) !== 'undefined' && !Array.isArray(ref[key])){ | ||
}else if(options.inlineArrays && typeof(ref[key]) !== 'undefined' && !Array.isArray(ref[key])){ | ||
ref[key] = [ref[key]]; | ||
@@ -146,3 +147,2 @@ } | ||
let outPart = out; | ||
console.log('parts', parts); | ||
const lastKey = parts.pop(); | ||
@@ -175,3 +175,3 @@ const unescapedLastKey = lastKey.replace(/\\\./g, '.'); | ||
// escapes the string val such that it is safe to be used as a key or value in an ini-file. Basically escapes quotes | ||
const safe = (val) => { | ||
const safe = (val, key, options = {}) => { | ||
// all kinds of values and keys | ||
@@ -181,2 +181,6 @@ if(typeof val !== 'string' || /[\n\r=]/.test(val) || /^\[/.test(val) || (val.length > 1 && isQuoted(val)) || val !== val.trim()){ | ||
} | ||
// force stringify certain keys | ||
if(key && options.forceStringifyKeys && options.forceStringifyKeys.includes(key)){ | ||
return JSON.stringify(val); | ||
} | ||
// comments | ||
@@ -238,3 +242,3 @@ return val.replace(/;/g, '\\;').replace(/#/g, '\\#'); | ||
safe, | ||
unsafe | ||
unsafe, | ||
}; |
{ | ||
"name": "@nodecraft/ini", | ||
"version": "2.1.2", | ||
"version": "2.2.0", | ||
"description": "An ini encoder/decoder for node", | ||
@@ -24,8 +24,8 @@ "repository": { | ||
"devDependencies": { | ||
"eslint": "^7.18.0", | ||
"eslint-config-nodecraft": "^7.2.0", | ||
"eslint-plugin-json": "^2.1.2", | ||
"@nodecraft/eslint-config": "^9.0.0", | ||
"eslint": "^7.30.0", | ||
"eslint-plugin-json": "^3.0.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-unicorn": "^26.0.1", | ||
"tap": "^14" | ||
"eslint-plugin-unicorn": "^34.0.1", | ||
"tap": "^15" | ||
}, | ||
@@ -32,0 +32,0 @@ "engines": { |
@@ -34,2 +34,10 @@ An ini format parser and serializer for node. | ||
## New `forceStringifyKeys` optoin | ||
Sometimes you need to write strings into an `ini` file with quotes around them, such as: | ||
```ini | ||
key="some string" | ||
``` | ||
By passing an array of `forceStringifyKeys`, you can specify which keys are forced stringified with `JSON.stringify` and therefore maintain their quotes. | ||
Note: This is pretty limited currently in that it doesn't account for the same key being in different sections, but covers our current use-case. | ||
## Usage | ||
@@ -36,0 +44,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
12830
212
146