-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PREAPPS-321 Fix bug with mapValuesDeep and add unit tests
Add preference keys to schema for displaying external images
- Loading branch information
1 parent
8bb1923
commit 480b492
Showing
8 changed files
with
2,053 additions
and
881 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
{ | ||
"presets": [ | ||
["@babel/env", { | ||
"modules": false | ||
}], | ||
"presets": [ | ||
["@babel/env", { | ||
"modules": false | ||
}], | ||
"@babel/typescript" | ||
], | ||
"plugins": [ | ||
"@babel/proposal-class-properties", | ||
"@babel/proposal-object-rest-spread", | ||
"lodash" | ||
], | ||
"env" : { | ||
"test": { | ||
"presets": [ | ||
"@babel/env", | ||
"@babel/typescript" | ||
], | ||
"plugins": [ | ||
"@babel/proposal-class-properties", | ||
"@babel/proposal-object-rest-spread", | ||
"lodash" | ||
] | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require("@babel/register")({ | ||
//by default, babel register does not pull in .ts extension | ||
extensions: [".js", ".ts"], | ||
}); |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,10 @@ | ||
import { isObject, mapValues } from 'lodash'; | ||
import { mapValues } from 'lodash'; | ||
|
||
export function mapValuesDeep( | ||
obj: {}, | ||
callback: (v: any, k: string) => any | ||
): {} { | ||
return mapValues(obj, (v, k) => { | ||
if (Array.isArray(v)) { | ||
return v.map(v2 => mapValuesDeep(v2, callback)); | ||
} else if (isObject(v)) { | ||
return mapValuesDeep(v, callback); | ||
} | ||
return callback(v, k); | ||
}); | ||
export function mapValuesDeep(obj: {}, callback: (v: any) => any): {} { | ||
if (typeof obj !== 'object') { | ||
return callback(obj); | ||
} else if (Array.isArray(obj)) { | ||
return obj.map(v => mapValuesDeep(v, callback)); | ||
} | ||
return mapValues(obj, v => mapValuesDeep(v, callback)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import chai, { expect } from 'chai'; | ||
import { mapValuesDeep } from '../../src/utils/map-values-deep.ts'; | ||
|
||
/* eslint-env mocha */ | ||
|
||
function cb(v) { | ||
if (typeof v === 'string') return '_' + v; | ||
if (typeof v === 'number') return v + 1; | ||
throw new Error('Should never see this'); | ||
} | ||
|
||
describe('utils/mapValuesDeep()', () => { | ||
it('should return string with callback applied when given string', () => { | ||
expect(mapValuesDeep('foo', cb)).to.equal('_foo'); | ||
}); | ||
|
||
it('should return number with callback applied when given number', () => { | ||
expect(mapValuesDeep(1, cb)).to.equal(2); | ||
}); | ||
|
||
it('should apply callback to each element of an array and return an array', () => { | ||
expect(mapValuesDeep(['foo', 1], cb)).to.eql(['_foo', 2]); | ||
}); | ||
|
||
it('should apply callback to each value of an object and return an object with same keys', () => { | ||
expect(mapValuesDeep({a: 'foo', b: 1}, cb)).to.eql({a: '_foo', b: 2}); | ||
}); | ||
|
||
it('should apply callback deeply', () => { | ||
expect(mapValuesDeep({ | ||
a: [ | ||
{ b: [ 'foo', {c: 1}]} | ||
] | ||
}, cb)).to.eql({ | ||
a: [ | ||
{ b: [ '_foo', {c: 2} ]} | ||
]}); | ||
}) | ||
}); |