easy-ini
Advanced tools
Comparing version 1.0.9 to 1.1.0
92
index.js
@@ -42,13 +42,7 @@ const _EMPTY_LINE = 0, | ||
_secArr(sec) {return sec[Object.keys(sec)[0]]} | ||
_secName(sec) {return Object.keys(sec)[0]} | ||
_getSection(obj, i) {return obj[i][Object.keys(obj[i])[0]]} | ||
_findType(line) {for (let t in this._types) {if (this._types[t](line)) {return t}}} | ||
_addByType(obj, type, key = '', val = '') { | ||
if (type == _SECTION) {obj.push({[key]:[]})} else { | ||
this._getSection(obj, obj.length -1).push({type, key, val}) | ||
if (type == _SECTION) {obj.push({name: key, content: []})} else { | ||
obj[obj.length -1].content.push({type, key, val}) | ||
} | ||
@@ -60,3 +54,5 @@ } | ||
const lines = string.split(this.eol) | ||
if (this._findType(lines[0]) != _SECTION) {iniData.push({[this._defSecName]:[]}) } | ||
if (this._findType(lines[0]) != _SECTION) { | ||
iniData.push({name: this._defSecName, content: []}) | ||
} | ||
for (let l of lines) { | ||
@@ -80,8 +76,7 @@ if (this.autoTrim) {l = l.trim()} | ||
let sectionString = '', contentString = '' | ||
const sName = this._secName(section) | ||
if (sName != this._defSecName && !noSections) { | ||
if (section.name != this._defSecName && !noSections) { | ||
sectionString += cleanEmptyLines ? this.eol : '' | ||
sectionString += shouldTrim ? `${sName.trim()}${this.eol}` : `${sName}${this.eol}` | ||
sectionString += shouldTrim ? `${section.name.trim()}${this.eol}` : `${section.name}${this.eol}` | ||
} | ||
for (const l of this._secArr(section)) { | ||
for (const l of section.content) { | ||
curLine = this._lineGens[l.type](l, shouldTrim, shouldFix) | ||
@@ -101,4 +96,4 @@ if (cleanEmptyLines && this._findType(curLine) == _EMPTY_LINE) {continue} | ||
for (const sec of this.iniData) { | ||
for (let line of this._secArr(sec)) { | ||
if(line.type == _PAIR && line.key.trim() == inputKey.trim()) { | ||
for (const line of sec.content) { | ||
if (line.type == _PAIR && line.key.trim() == inputKey.trim()) { | ||
return line | ||
@@ -122,5 +117,5 @@ } | ||
for (const sec of this.iniData) { | ||
for (const [index, line] of this._secArr(sec).entries()) { | ||
for (const [index, line] of sec.content.entries()) { | ||
if (line.type == _PAIR && line.key.trim() == inputKey.trim()) { | ||
this._secArr(sec).splice(index, 1) | ||
sec.content.splice(index, 1) | ||
return true | ||
@@ -135,4 +130,4 @@ } | ||
for (const [index, section] of this.iniData.entries()) { | ||
if (sections.includes(this._secName(section)) | ||
|| (partialMatch && sections.filter(a => this._secName(section).indexOf(a) >= 0).length > 0)) { | ||
if (sections.includes(section.name) | ||
|| (partialMatch && sections.filter(a => section.name.indexOf(a) >= 0).length > 0)) { | ||
continue | ||
@@ -149,4 +144,4 @@ } else { | ||
for (const [index, section] of this.iniData.entries()) { | ||
if (!partialMatch && this._secName(section) == sectionName || | ||
partialMatch && this._secName(section).indexOf(sectionName) >= 0) { | ||
if (!partialMatch && section.name == sectionName || | ||
partialMatch && section.name.indexOf(sectionName) >= 0) { | ||
this.iniData.splice(index, 1) | ||
@@ -168,6 +163,4 @@ return true | ||
for (const sec of this.iniData) { | ||
const sName = this._secName(sec) | ||
if (sectionName == sName.trim()) { | ||
const lines = this._secArr(sec) | ||
for (const line of lines) { | ||
if (sectionName == sec.name.trim()) { | ||
for (const line of sec.content) { | ||
if (line.key == stringObj.key && stringObj.type == _PAIR) { | ||
@@ -180,3 +173,3 @@ line.val = stringObj.val | ||
if (!valueAdded) { | ||
lines.push(stringObj) | ||
sec.content.push(stringObj) | ||
existingSec = true | ||
@@ -188,3 +181,3 @@ break | ||
if (!existingSec && !valueAdded) { | ||
this.iniData.push({[sectionName]:[]}) | ||
this.iniData.push({name: sectionName, content: []}) | ||
return this.putStringInSection(string, sectionName) | ||
@@ -198,5 +191,4 @@ } | ||
for (const sec of this.iniData) { | ||
const sLines = this._secArr(sec) | ||
for (const line of sLines) { | ||
if(line.type == _PAIR) { | ||
for (const line of sec.content) { | ||
if (line.type == _PAIR) { | ||
if (line.val.indexOf(token) >= 0 || line.key.indexOf(token) >= 0) { | ||
@@ -218,7 +210,6 @@ resultLines.push(this._lineGens[line.type](line)) | ||
for (const sec of this.iniData) { | ||
const sLines = this._secArr(sec) | ||
for (const [index, line] of sLines.entries()) { | ||
if(line.type == _PAIR) { | ||
for (const [index, line] of sec.content.entries()) { | ||
if (line.type == _PAIR) { | ||
if (line.val.indexOf(token) >= 0 || line.key.indexOf(token) >= 0) { | ||
sLines.splice(index, 1) | ||
sec.content.splice(index, 1) | ||
if (global) {return this.removeLineByMatch(token, global, true)} | ||
@@ -229,3 +220,3 @@ else {_done = true; break} | ||
if (line.key.indexOf(token) >= 0) { | ||
sLines.splice(index, 1) | ||
sec.content.splice(index, 1) | ||
if (global) {return this.removeLineByMatch(token, global, true)} | ||
@@ -241,4 +232,3 @@ else {_done = true; break} | ||
for (const sec of this.iniData) { | ||
const sLines = this._secArr(sec) | ||
for (const line of sLines) { | ||
for (const line of sec.content) { | ||
if (line.type == _PAIR) { | ||
@@ -263,5 +253,4 @@ if (line.val.indexOf(token) >= 0) { | ||
for (const sec of this.iniData) { | ||
const sLines = this._secArr(sec) | ||
for (const [index, line] of sLines.entries()) { | ||
if(line.type == _PAIR) { | ||
for (const [index, line] of sec.content.entries()) { | ||
if (line.type == _PAIR) { | ||
if (preferFirstOccurrence) { | ||
@@ -276,3 +265,3 @@ const tempKey = line.key | ||
if (this.getKeyIfExists(tempKey)) { | ||
sLines.splice(index, 1) | ||
sec.content.splice(index, 1) | ||
return this.solveDuplicates(preferFirstOccurrence) | ||
@@ -293,4 +282,3 @@ } else { | ||
for (const sec of this.iniData) { | ||
const sLines = this._secArr(sec) | ||
for (const line of sLines) { | ||
for (const line of sec.content) { | ||
if (line.type == _PAIR) { | ||
@@ -326,8 +314,6 @@ const matchRes = line.val.match(refReg) | ||
sectionFound = false | ||
const s2Name = this._secName(s2), s2Lines = this._secArr(s2) | ||
for (const s of this.iniData) { | ||
const sName = this._secName(s), sLines = this._secArr(s) | ||
if (s2Name == sName) { | ||
if (before) {sLines.unshift(...s2Lines.map(a => ({...a})))} | ||
else {sLines.push(...s2Lines.map(a => ({...a})))} | ||
if (s2.name == s.name) { | ||
if (before) {s.content.unshift(...s2.content.map(a => ({...a})))} | ||
else {s.content.push(...s2.content.map(a => ({...a})))} | ||
sectionFound = true | ||
@@ -339,7 +325,8 @@ break | ||
if (before) { | ||
this.iniData.splice(1, 0, {[s2Name]:[]}); | ||
this.iniData[1][s2Name].push(...s2Lines.map(a => ({...a}))) | ||
const index = this.iniData[0].name == this._defSecName ? 1 : 0 | ||
this.iniData.splice(index, 0, {name: s2.name, content: []}); | ||
this.iniData[index].content.push(...s2.content.map(a => ({...a}))) | ||
} else { | ||
this.iniData.push({[s2Name]:[]}) | ||
this.iniData[this.iniData.length -1][s2Name].push(...s2Lines.map(a => ({...a}))) | ||
this.iniData.push({name: s2.name, content: []}) | ||
this.iniData[this.iniData.length -1].content.push(...s2.content.map(a => ({...a}))) | ||
} | ||
@@ -351,2 +338,3 @@ } | ||
} | ||
module.exports = INIClass |
{ | ||
"name": "easy-ini", | ||
"version": "1.0.9", | ||
"version": "1.1.0", | ||
"description": "manage ini files content", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
25
test.js
const INI = require('./index') | ||
const myINI = new INI(`[INCLUDES]\n#INCLUDE=config.ini\n\nOUT_PE=\\Release\\%INST_NAME%_Downloader_v%VI_FILE_VER%_%__cdate%_unsigned.exe\n`) | ||
const myINI = new INI(`[INCLUDES]\n#INCLUDE=config.ini\n\n\n[ok]\nthing=thang\ncool=#thing%\n[WOW]\nname=gal\nthing=111\nOUT_PE=\\Release\\%INST_NAME%_Downloader_v%VI_FILE_VER%_%__cdate%_unsigned.exe\n`) | ||
console.log(myINI.createINIString()) | ||
console.log('111111111122222222233333334444444') | ||
const myINI2 = new INI(`[INCLUDES]\n#INCLUDE=config.ini\n\nxzczxcxzc\n[ok]\nthing=thang\ncool=#thing%\n[WOW]\nname=gal\nthing=111\nOUT_PE=\\Release\\%INST_NAME%_Downloader_v%VI_FILE_VER%_%__cdate%_unsigned.exe\n`) | ||
myINI.mergeWith(myINI2, true) | ||
console.log(myINI.createINIString()) | ||
/* super_secret_config.ini: | ||
@@ -8,6 +14,9 @@ something=whatever | ||
relax=%something% */ | ||
console.log(myINI.getKeyIfExists('OUT_PE')) | ||
console.log(myINI.findAndRemoveKeyIfExists('OUT_PE')) | ||
console.log(myINI.getKeyIfExists('OUT_PE')) | ||
myINI.findAndRemoveSectionIfExists(['[INCLUDES]']) | ||
myINI.putStringInSection('cool=not') | ||
myINI.putStringInSection('#cool=not') | ||
myINI.putStringInSection('something=whatever','[test]') | ||
myINI.putStringInSection('something=whatev1234er','[test]') | ||
myINI.putStringInSection('say=ha','[test]') | ||
@@ -22,5 +31,9 @@ myINI.putStringInSection('dude=%say%%say%%say% %relax%','[test]') | ||
myINI.putStringInSection('fuck=%need%_%need%','[CERT SYS]') | ||
myINI.findAndRemoveSectionIfExists('SYS', false) | ||
myINI.findAndReplace('1234', 'XXX', true) | ||
// console.log(myINI.getLinesByMatch('fu')) | ||
// console.log(myINI.removeLineByMatch('fu',true)) | ||
console.log(myINI.solveDuplicates()) | ||
console.log('111111111122222222233333334444444') | ||
myINI.solveSelfReferences('%','%') | ||
myINI.removeEverythingButSections(['[INCLUDES]']) | ||
console.log(myINI.createINIString()) | ||
console.log(myINI.createINIString({cleanBreakComment:true, cleanEmptyLines:true, noEmptySections:true, shouldTrim:true, shouldFix:true})) | ||
// myINI.findAndRemoveSectionIfExists('SYS', false) |
58151
349