Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move libraries to a separate modules #309

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
10 changes: 5 additions & 5 deletions bytecode/bytecode-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ module.exports.$keyBy = function keyBy($offset, $length) {
this.collectionFunction();
const key = '' + this.$stack.pop();
$cache.indexToKey[index] = key;
$cache.keyToIndices[key] = $cache.keyToIndices[key] || new Set();
$cache.keyToIndices[key] = $cache.keyToIndices[key] || new Set()
$cache.keyToIndices[key].add(index);
this.setOnObject($out, key, src[index], $new);
}
Expand All @@ -509,7 +509,7 @@ module.exports.$keyBy = function keyBy($offset, $length) {
const key = '' + this.$stack.pop();
$cache.indexToKey[index] = key;
keysPendingDelete.delete(key);
$cache.keyToIndices[key] = $cache.keyToIndices[key] || new Set();
$cache.keyToIndices[key] = $cache.keyToIndices[key] || new Set()
$cache.keyToIndices[key].add(index);
this.setOnObject($out, key, src[index], $new);
}
Expand Down Expand Up @@ -882,10 +882,10 @@ module.exports.$groupBy = function groupBy($offset, $length) {
this.setOnObject($out[res], key, src[key], $new);
});
} else {
const keysPendingDelete = {};
const keysPendingDelete = Object.create(null);
$invalidatedKeys.forEach(key => {
if ($keyToKey[key]) {
keysPendingDelete[$keyToKey[key]] = keysPendingDelete[$keyToKey[key]] || new Set();
keysPendingDelete[$keyToKey[key]] = keysPendingDelete[$keyToKey[key]]|| new Set()
keysPendingDelete[$keyToKey[key]].add(key);
}
});
Expand All @@ -907,7 +907,7 @@ module.exports.$groupBy = function groupBy($offset, $length) {
this.setOnObject($out[res], key, src[key], $new);
this.setOnObject($out, res, $out[res], $new);

if (keysPendingDelete.hasOwnProperty(res)) {
if (keysPendingDelete[res]) {
keysPendingDelete[res].delete(key);
}
});
Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/arrays.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require('path')
const {compile, and, or, root, arg0, arg1, setter, splice, bind, chain} = require('../../index');
const {
describeCompilers,
Expand Down Expand Up @@ -163,7 +164,8 @@ describe('testing array', () => {
setArr: setter('arr', arg0),
setCompareTo: setter('compareTo')
};
const optModel = evalOrLoad(compile(model, {compiler}));
const compiled = compile(model, {compiler});
const optModel = evalOrLoad(compiled.replace ? compiled.replace('carmi/src/lib', path.resolve(__dirname, '../lib')) : compiled);
const inst = optModel({arr: [0, 1, 2, 3, 4], compareTo: 2}, funcLibrary);
expect(currentValues(inst)).toEqual({
greaterThan: [false, false, false, true, true],
Expand Down
2,481 changes: 1,242 additions & 1,239 deletions src/babelPlugin/__snapshots__/macro-debug.spec.js.snap

Large diffs are not rendered by default.

2,481 changes: 1,242 additions & 1,239 deletions src/babelPlugin/__snapshots__/macro-no-current-line.spec.js.snap

Large diffs are not rendered by default.

2,481 changes: 1,242 additions & 1,239 deletions src/babelPlugin/__snapshots__/macro.spec.js.snap

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/bytecode-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ ${defineEnum('setterTypes', setterTypes)}
// }

// root_type Bytecode;
//
//

`;
fs.writeFileSync(path.join(__dirname, '..', 'bytecode', 'bytecode-enums.js'), enums);
Expand Down Expand Up @@ -310,8 +310,8 @@ const visitorsPointFunctionsToThis = {

const functionsById = functions.reduce((acc, f) => ({...acc, [f.id.name]: f}), {});
const verbFunctions = Object.keys(functionsById).reduce((acc, name) => {
if (verbsSet.has(name) || verbsSet.has(name.replace('Opt', ''))) {
return {...acc, [verbsSet.has(name) ? name : name.replace('Opt', '')]: functionsById[name]};
if (verbsSet.has(name)) {
return {...acc, [name]: functionsById[name]};
}
return acc;
}, {});
Expand Down Expand Up @@ -378,7 +378,7 @@ const snippets = _.mapValues(
} else {
this.$stack.push(0);
}
},
},
start: ($offset, $length) => {
const start = this.$stack.pop();
},
Expand Down
3 changes: 2 additions & 1 deletion src/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = (model, options) => {
return JSON.stringify(compiler.getters, null, 2);
}
const rawSource = compiler.compile();
const imports = compiler.importLibrary();
let source = rawSource;
if (options.prettier && typeof source === 'string') {
try {
Expand All @@ -53,7 +54,7 @@ module.exports = (model, options) => {
let result;

if (compiler.lang === 'js' && typeof source === 'string') {
result = wrapModule(options.format, source, options.name);
result = wrapModule(options.format, source, options.name, imports);
} else {
result = source;
}
Expand Down
192 changes: 192 additions & 0 deletions src/lib/naive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/* eslint-disable arrow-body-style */

const {createUtils} = require('./utils')

const createLibrary = (model) => {
const {
ensurePath,
getAssignableObject,
applySetter
} = createUtils(model)

const mapValues = (func, src, context) => {
return Object.keys(src).reduce((acc, key) => {
acc[key] = func(src[key], key, context);
return acc;
}, {});
}

const filterBy = (func, src, context) => {
return Object.keys(src).reduce((acc, key) => {
if (func(src[key], key, context)) {
acc[key] = src[key];
}
return acc;
}, {});
}

const groupBy = (func, src, context) => {
if (Array.isArray(src)) {
throw new Error('groupBy only works on objects');
}
return Object.keys(src).reduce((acc, key) => {
const newKey = func(src[key], key, context);
acc[newKey] = acc[newKey] || {};
acc[newKey][key] = src[key];
return acc;
}, {});
}

const mapKeys = (func, src, context) => {
return Object.keys(src).reduce((acc, key) => {
const newKey = func(src[key], key, context);
acc[newKey] = src[key];
return acc;
}, {});
}

const map = (func, src, context) => {
return src.map((val, key) => func(val, key, context));
}

const any = (func, src, context) => {
return src.some((val, key) => func(val, key, context));
}

const filter = (func, src, context) => {
return src.filter((val, key) => func(val, key, context));
}

const anyValues = (func, src, context) => {
return Object.keys(src).some(key => func(src[key], key, context));
}

const keyBy = (func, src, context) => {
return src.reduce((acc, val, key) => {
acc[func(val, key, context)] = val;
return acc;
}, {});
}

const keys = (src) => {
return Array.from(Object.keys(src));
}

const values = (src) => {
return Array.isArray(src) ? src : Array.from(Object.values(src));
}

const assign = (src) => {
return Object.assign({}, ...src);
}

const size = (src) => {
return Array.isArray(src) ? src.length : Object.keys(src).length;
}

const isEmpty = (src) => {
return Array.isArray(src) ? src.length === 0 : Object.keys(src).length === 0;
}

const last = (src) => {
return src[src.length - 1];
}

const range = (end, start = 0, step = 1) => {
const res = [];
// eslint-disable-next-line no-unmodified-loop-condition
for (let val = start; step > 0 && val < end || step < 0 && val > end; val += step) {
res.push(val);
}
return res;
}

const defaults = (src) => {
return Object.assign({}, ...[...src].reverse());
}

const loopFunction = (resolved, res, func, src, context, key) => {
if (!resolved[key]) {
resolved[key] = true;
res[key] = src.hasOwnProperty(key) ? func(src[key], key, context, loopFunction.bind(null, resolved, res, func, src, context)) : undefined;
}
return res[key];
}

const sum = (src) => {
return src.reduce((sum, val) => sum + val, 0)
}

const flatten = (src) => {
return [].concat(...src)
}

const recursiveMap = (func, src, context) => {
const res = [];
const resolved = src.map(x => false);
src.forEach((val, key) => {
loopFunction(resolved, res, func, src, context, key);
});
return res;
}

const recursiveMapValues = (func, src, context) => {
const res = {};
const resolved = {};
Object.keys(src).forEach(key => resolved[key] = false);
Object.keys(src).forEach(key => {
loopFunction(resolved, res, func, src, context, key);
});
return res;
}

const set = (path, value) => {
ensurePath(path)
applySetter(getAssignableObject(path, path.length - 1), path[path.length - 1], value)
}

const splice = (pathWithKey, len, ...newItems) => {
ensurePath(pathWithKey)
const key = pathWithKey[pathWithKey.length - 1]
const path = pathWithKey.slice(0, pathWithKey.length - 1)
const arr = getAssignableObject(path, path.length)
arr.splice(key, len, ...newItems)
}

const push = (path, value) => {
ensurePath([...path, 0])
const arr = getAssignableObject(path, path.length)
splice([...path, arr.length], 0, value)
}


return {
any,
anyValues,
assign,
defaults,
filter,
filterBy,
flatten,
groupBy,
isEmpty,
keyBy,
keys,
last,
loopFunction,
map,
mapKeys,
mapValues,
push,
range,
recursiveMap,
recursiveMapValues,
set,
size,
splice,
sum,
values
}
}

module.exports = {createLibrary};
Loading