Skip to content

Commit 6d1ff0d

Browse files
committed
feat: support array push syntax
1 parent 085bc2f commit 6d1ff0d

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ db.enabled=true
4343
update({ 'db.enabled': true }) // or update(..., { name: '.conf' })
4444
```
4545

46+
Push to an array:
47+
48+
```ts
49+
update({ 'modules[]': 'test' })
50+
```
51+
4652
**Read/Write config:**
4753

4854
```ts
@@ -83,6 +89,8 @@ It means that you can use `.` for keys to define objects. Some examples:
8389

8490
**Note:** If you use keys that can override like `x=` and `x.y=`, you can disable this feature by passing `flat: true` option.
8591

92+
**Tip:** You can use keys ending with `[]` to push to an array like `test[]=A`
93+
8694
## Native Values
8795

8896
RC uses [destr](https://www.npmjs.com/package/destr) to convert values into native javascript values.

src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,20 @@ export function parse (contents: string, options?: RCOptions): RC {
4040

4141
// Key
4242
const key = match[1]
43-
if (!key || key === '__proto__' || key === 'constructor') { continue }
44-
config[key] = destr(match[2].trim() /* val */)
43+
44+
if (!key || key === '__proto__' || key === 'constructor') {
45+
continue
46+
}
47+
48+
const val = destr(match[2].trim() /* val */)
49+
50+
if (key.endsWith('[]')) {
51+
const nkey = key.substr(0, key.length - 2)
52+
config[nkey] = (config[nkey] || []).concat(val)
53+
continue
54+
}
55+
56+
config[key] = val
4557
}
4658

4759
return options?.flat ? config : unflatten(config, { overwrite: true })

test/index.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,15 @@ describe('rc', () => {
5353
update(obj, { flat: true, name: '.conf2' })
5454
expect(read({ flat: true, name: '.conf2' })).toMatchObject(obj)
5555
})
56+
57+
test('Parse indexless arrays', () => {
58+
expect(parse(`
59+
x.foo[]=A
60+
x.foo[]=B
61+
`)).toMatchObject({
62+
x: {
63+
foo: ['A', 'B']
64+
}
65+
})
66+
})
5667
})

0 commit comments

Comments
 (0)