Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: duplicate properties in diff section
  • Loading branch information
wujunchuan committed Nov 28, 2020
commit e26e71c9d918f1189103fdc707abd29d99a52c1d
12 changes: 9 additions & 3 deletions ini.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ function decode (str) {
var re = /^\[([^\]]*)\]$|^([^=]+)(=(.*))?$/i
var lines = str.split(/[\r\n]+/g)
// for duplicate property statist
var bucket = {}
var bucket = {
'root': {}
}
var curSection = 'root';

lines.forEach(function (line, _, __) {
if (!line || line.match(/^\s*[;#]/)) return
Expand All @@ -84,15 +87,18 @@ function decode (str) {
if (match[1] !== undefined) {
section = unsafe(match[1])
p = out[section] = out[section] || {}
bucket[section] = bucket[section] || {}
curSection = section;
return
}
var key = unsafe(match[2])
var value = match[3] ? unsafe(match[4]) : true

var _keyWithoutArraySuffix = key.split('[]')[0]
bucket[_keyWithoutArraySuffix] = bucket[_keyWithoutArraySuffix] ? ++bucket[_keyWithoutArraySuffix] : 1
var curBucket = bucket[curSection];
curBucket[_keyWithoutArraySuffix] = curBucket[_keyWithoutArraySuffix] ? ++curBucket[_keyWithoutArraySuffix] : 1

if (bucket[_keyWithoutArraySuffix] > 1) key = _keyWithoutArraySuffix + '[]'
if (curBucket[_keyWithoutArraySuffix] > 1) key = _keyWithoutArraySuffix + '[]'

switch (value) {
case 'true':
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/foo.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ar[] = three
; This should be included in the array
ar = this is included

; Test resetting of a value (and not turn it into an array)
; Test resetting of a value (turn it into an array)
br = cold
br = warm

Expand Down
5 changes: 3 additions & 2 deletions test/foo.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var i = require("../")
+ 'ar[]=one\n'
+ 'ar[]=three\n'
+ 'ar[]=this is included\n'
+ 'br=warm\n'
+ 'br[]=cold\n'
+ 'br[]=warm\n'
+ 'eq=\"eq=eq\"\n'
+ '\n'
+ '[a]\n'
Expand All @@ -43,7 +44,7 @@ var i = require("../")
's2': 'something else',
'zr': ['deedee'],
'ar': ['one', 'three', 'this is included'],
'br': 'warm',
'br': ['cold', 'warm'],
'eq': 'eq=eq',
a:
{ av: 'a val',
Expand Down