hughsk / hughsk/flat

Add transformFirst option to allow modification of key to provide first key capability

Open
#107 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.8k
Forks
197
PR merge metrics
No merged PRs in 30d

Description

I will be submitting a PR momentarily.

Example code for flatten, including CURRENT transformKey:

```
test('Transformed Keys', function () {
assert.deepStrictEqual(flatten({
hello: {
world: {
again: 'good morning'
}
},
lorem: {
ipsum: {
dolor: 'good evening'
}
}
}, {
transformKey: function (key) {
return '__' + key + '__'
}
}), {
'__hello__.__world__.__again__': 'good morning',
'__lorem__.__ipsum__.__dolor__': 'good evening'
})
})

test('Transformed First Key', function () {
assert.deepStrictEqual(flatten({
hello: {
world: {
again: 'good morning'
}
},
lorem: {
ipsum: {
dolor: 'good evening'
}
}
}, {
transformFirst: function (key) {
return '__' + key + '__'
}
}), {
'__hello__.world.again': 'good morning',
'__lorem__.ipsum.dolor': 'good evening'
})
})

test('Transformed First Key with Custom Delimiter', function () {
assert.deepStrictEqual(flatten({
hello: {
world: {
again: 'good morning'
}
},
lorem: {
ipsum: {
dolor: 'good evening'
}
}
}, {
transformFirst: function (key) {
return '__' + key + '__'
},
delimiter: ':'
}), {
'__hello__:world:again': 'good morning',
'__lorem__:ipsum:dolor': 'good evening'
})
})

test('Transformed First Key to Add Prefix with Default Delimiter', function () {
assert.deepStrictEqual(flatten({
hello: {
world: {
again: 'good morning'
}
},
lorem: {
ipsum: {
dolor: 'good evening'
}
}
}, {
transformFirst: function (key, delim) {
return 'foo' + delim + key
}
}), {
'foo.hello.world.again': 'good morning',
'foo.lorem.ipsum.dolor': 'good evening'
})
})

test('Transformed First Key to Add Prefix with Custom Delimiter', function () {
assert.deepStrictEqual(flatten({
hello: {
world: {
again: 'good morning'
}
},
lorem: {
ipsum: {
dolor: 'good evening'
}
}
}, {
transformFirst: function (key, delim) {
return 'foo' + delim + key
},
delimiter: ':'
}), {
'foo:hello:world:again': 'good morning',
'foo:lorem:ipsum:dolor': 'good evening'
})
})

test('Transformed Results and Keys', function () {
assert.deepStrictEqual(flatten({
hello: {
world: {
again: 'good morning'
}
},
lorem: {
ipsum: {
dolor: 'good evening'
}
}
}, {
transformKey: function (key) {
return '__' + key + '__'
},
transformFirst: function (key, delim) {
return 'translated' + delim + key
}
}), {
'translated.__hello__.__world__.__again__': 'good morning',
'translated.__lorem__.__ipsum__.__dolor__': 'good evening'
})
})
```

Example code for unflatten:

``` test('Transformed First Key', function () {
assert.deepStrictEqual(unflatten({
'foo.hello.world.again': 'good morning',
'foo.lorem.ipsum.dolor': 'good evening'
}, {
transformFirst: function (key, delim) {
return key.substring(('foo'+delim).length)
}
}), {
hello: {
world: {
again: 'good morning'
}
},
lorem: {
ipsum: {
dolor: 'good evening'
}
}
})
})

test('Transformed First Key and Subsequent Keys', function () {
assert.deepStrictEqual(unflatten({
'foo.__hello__.__world__.__again__': 'good morning',
'foo.__lorem__.__ipsum__.__dolor__': 'good evening'
}, {
transformKey: function (key) {
return key.substring(2, key.length - 2)
},
transformFirst: function (key, delim) {
return key.substring(('foo'+delim).length)
}
}), {
hello: {
world: {
again: 'good morning'
}
},
lorem: {
ipsum: {
dolor: 'good evening'
}
}
})
})
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by locating the flatten and unflatten entry points and their existing tests, then compare their option handling with the examples in this issue. The work is done when transformFirst behaves as shown for default and custom delimiters, including combinations with transformKey, and the unflatten examples pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.