Tore out the old engine, left some wounds

This commit is contained in:
Stefan Zermatten
2021-09-27 14:28:32 +02:00
parent 2228802dd3
commit fdea748441
125 changed files with 102 additions and 2235 deletions

View File

@@ -0,0 +1,51 @@
import { get } from 'lodash';
export default function applyFnToKey(doc, key, fn){
if (key.includes('.$')){
applyToArrayKey(doc, key, fn);
} else {
applyToSingleKey(doc, key, fn);
}
}
function applyToSingleKey(doc, key, fn){
// call the function with the current value and document for context
fn(doc, key);
}
/**
* 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,
paths: keySplit.slice(1),
currentPath: keySplit[0],
indices: [],
}];
while(stack.length){
const state = stack.pop();
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,
paths: state.paths.slice(1),
currentPath,
indices: [...state.indices, index],
});
}
}
}
}

View File

@@ -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',
]
}]
}
}

View File

@@ -0,0 +1,6 @@
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
export default function cleanProp(prop){
let schema = CreatureProperties.simpleSchema(prop);
return schema.clean(prop);
}

View File

@@ -0,0 +1,10 @@
export default function findAncestorByType(prop, type, propsById){
if (!prop || !prop.ancestors) return;
let ancestor;
for (let i = prop.ancestors.length - 1; i >= 0; i--){
ancestor = propsById[prop.ancestors[i].id];
if (ancestor && ancestor.type === type){
return ancestor;
}
}
}

View File

@@ -0,0 +1,3 @@
export default function stripFloatingPointOddities(num, precision = 12){
return +parseFloat(num.toPrecision(precision))
}

View File

@@ -0,0 +1,8 @@
export default function walkDown(tree, callback){
let stack = [...tree];
while(stack.length){
let node = stack.pop();
callback(node);
stack.push(...node.children);
}
}