Commit 6ee7c2c6 authored by Bryan Tong's avatar Bryan Tong
Browse files

Merge branch 'mapperMap' into 'master'

Add Mapper.map

Closes #104

See merge request kado/kado!311
parents c4e1b4cd 82156898
Pipeline #8496 passed with stage
in 2 minutes and 32 seconds
......@@ -21,6 +21,19 @@ creating a full instance.
Instantiates a new empty `Mapper` instance, optionally populated with `data`
### `Mapper.map (iterator)`
* `iterator` {Function} a method that takes `value` and `key` as arguments.
* Returns: {Mapper} the original object.
Example
```js
const someObject = { foo: 'baz', two: 2, three: null }
Mapper.getInstance(someObject).map((value, key) => {
if (value === null) value = ''
return value
})
```
### `Mapper.merge (data)`
* `data` {Object} optional data to populate
* Returns: {Mapper} the same as the called `Mapper`, for chaining
......
......@@ -22,6 +22,24 @@ const Assert = require('./Assert')
module.exports = class Mapper {
static getInstance (data) { return new Mapper(data) }
static deleteFromObject (base, keyArray, ctx = null) {
const key = keyArray.shift()
if (key && keyArray.length === 0) {
if (!ctx) ctx = base
delete ctx[key]
return base
}
if (!ctx) ctx = base
return Mapper.deleteFromObject(base, keyArray, ctx[key])
}
static getFromObject (base, keyArray) {
const key = keyArray.shift()
if (key && keyArray.length === 0) return base[key]
return Mapper.getFromObject(base[key], keyArray)
}
static mergeObject (base, overlay, depth = 0, maxDepth = 50) {
// define base if needed
if (base === undefined) {
......@@ -47,10 +65,16 @@ module.exports = class Mapper {
return base
}
static getFromObject (base, keyArray) {
const key = keyArray.shift()
if (key && keyArray.length === 0) return base[key]
return Mapper.getFromObject(base[key], keyArray)
static parseKey (key) {
if (key instanceof Array) return key
if (typeof key === 'string') {
key = key.split('.')
}
if (typeof key === 'function') {
key = key()
}
if (!(key instanceof Array)) key = [key]
return key
}
static setToObject (base, keyArray, value) {
......@@ -65,47 +89,14 @@ module.exports = class Mapper {
return base
}
static deleteFromObject (base, keyArray, ctx = null) {
const key = keyArray.shift()
if (key && keyArray.length === 0) {
if (!ctx) ctx = base
delete ctx[key]
return base
}
if (!ctx) ctx = base
return Mapper.deleteFromObject(base, keyArray, ctx[key])
}
static parseKey (key) {
if (key instanceof Array) return key
if (typeof key === 'string') {
key = key.split('.')
}
if (typeof key === 'function') {
key = key()
}
if (!(key instanceof Array)) key = [key]
return key
}
constructor (data) {
this.merge(data)
}
merge (data) {
Mapper.mergeObject(this, data || {})
all () {
return this
}
get (key) {
return Mapper.getFromObject(this, Mapper.parseKey(key))
}
set (key, value) {
Mapper.setToObject(this, Mapper.parseKey(key), value)
return value
}
delete (key) {
const keys = Mapper.parseKey(key)
const rv = keys[keys.length - 1]
......@@ -113,7 +104,25 @@ module.exports = class Mapper {
return rv
}
all () {
get (key) {
return Mapper.getFromObject(this, Mapper.parseKey(key))
}
map (iterator) {
if (!iterator) return this
Object.keys(this).map((key) => {
return iterator(this.get(key), key)
})
return this
}
merge (data) {
Mapper.mergeObject(this, data || {})
return this
}
set (key, value) {
Mapper.setToObject(this, Mapper.parseKey(key), value)
return value
}
}
......@@ -26,6 +26,18 @@ runner.suite('Mapper', (it) => {
it('should construct', () => {
Assert.isType('Mapper', new Mapper())
})
it('should map an object', () => {
const productList = { test1: 'foo', test2: 'bar', test3: 'baz' }
const keyList = ['test1', 'test2', 'test3']
const valueList = ['foo', 'bar', 'baz']
Mapper.getInstance(productList).map((value, key) => {
const keyTest = keyList.shift()
const valueTest = valueList.shift()
Assert.isOk(key === keyTest, 'Failed key match')
Assert.isOk(value === valueTest, 'Failed value match')
return value
})
})
it('should set a value with a string', () => {
Assert.eq(mapper.set('foo1', 'bar'), 'bar')
})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment