Got tests running on single property character
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { get } from 'lodash';
|
||||
|
||||
export function applyFnToKey(doc, key, fn){
|
||||
if (key.includes('$.')){
|
||||
export default function applyFnToKey(doc, key, fn){
|
||||
if (key.includes('.$')){
|
||||
applyToArrayKey(doc, key, fn);
|
||||
} else {
|
||||
applyToSingleKey(doc, key, fn);
|
||||
@@ -16,26 +16,31 @@ function applyToSingleKey(doc, key, fn){
|
||||
/**
|
||||
* Applies the given function to all instances in a document key
|
||||
* key.$.with.$.subdocs will apply to all key[i...n].with[j...m].subdocs
|
||||
* Warning: Order might be confusing, it will traverse the deepest array in order
|
||||
* but the shallower arrays in reverse order
|
||||
*/
|
||||
function applyToArrayKey(doc, key, fn){
|
||||
const keySplit = key.split('.$');
|
||||
|
||||
// Stack based depth first traversal of arrays
|
||||
const array = get(doc, keySplit[0]);
|
||||
if (!array) return;
|
||||
const stack = [{
|
||||
array: get(doc, keySplit[0]),
|
||||
array,
|
||||
paths: keySplit.slice(1),
|
||||
currentPath: keySplit[0],
|
||||
indices: [],
|
||||
}];
|
||||
while(stack.length){
|
||||
const state = stack.pop();
|
||||
for (let index in state.array.length){
|
||||
for (let index in state.array){
|
||||
const currentPath = `${state.currentPath}[${index}]${state.paths[0]}`
|
||||
if (state.paths.length == 1){
|
||||
applyToSingleKey(doc, currentPath, fn);
|
||||
} else {
|
||||
const array = get(doc, currentPath);
|
||||
if (!array) return;
|
||||
stack.push({
|
||||
array: get(doc, currentPath),
|
||||
array,
|
||||
paths: state.paths.slice(1),
|
||||
currentPath,
|
||||
indices: [...state.indices, index],
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import applyFnToKey from './applyFnToKey.js';
|
||||
import { assert } from 'chai';
|
||||
import { get } from 'lodash';
|
||||
|
||||
describe('apply function to key', function(){
|
||||
it('uses a basic key correctly', function(){
|
||||
let obj = getStartingObject();
|
||||
applyFnToKey(obj, 'fox.name', (doc, key) => {
|
||||
assert.equal(obj, doc);
|
||||
assert.equal(key, 'fox.name');
|
||||
assert.equal(get(doc, key), 'foxy');
|
||||
});
|
||||
});
|
||||
it('uses a single nested key correctly', function(){
|
||||
let obj = getStartingObject();
|
||||
let foxSounds = [];
|
||||
applyFnToKey(obj, 'fox.sound.$', (doc, key) => {
|
||||
foxSounds.push(get(doc, key));
|
||||
});
|
||||
assert.include(foxSounds, 'wah');
|
||||
assert.include(foxSounds, 'tjoef');
|
||||
assert.include(foxSounds, 'kek');
|
||||
});
|
||||
it('uses a double nested key correctly', function(){
|
||||
let obj = getStartingObject();
|
||||
let birdSounds = [];
|
||||
applyFnToKey(obj, 'birds.$.sound.$', (doc, key) => {
|
||||
birdSounds.push(get(doc, key));
|
||||
});
|
||||
assert.include(birdSounds, 'koer');
|
||||
assert.include(birdSounds, 'hello');
|
||||
assert.include(birdSounds, 'squawk');
|
||||
});
|
||||
});
|
||||
|
||||
function getStartingObject(){
|
||||
return {
|
||||
fox: {
|
||||
name: 'foxy',
|
||||
sound: [
|
||||
'tjoef',
|
||||
'kek',
|
||||
'wah'
|
||||
]
|
||||
},
|
||||
birds: [{
|
||||
name: 'pigeon',
|
||||
sound: [
|
||||
'koer',
|
||||
]
|
||||
},{
|
||||
name: 'parrot',
|
||||
sound: [
|
||||
'hello',
|
||||
'cracker?',
|
||||
'squawk',
|
||||
]
|
||||
}]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user