Skip to content

Commit

Permalink
Added configure method for configuring flags and tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
marziply committed Aug 30, 2021
1 parent 02e586f commit 1a8be0c
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 65 deletions.
59 changes: 32 additions & 27 deletions dist/yank.js → dist/yank.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,11 @@ var Filter = function () {

this.filter = filter;
this.key = key;
this.params = params;
this.name = name;
this.params = params;
this.fn = filters[this.name];
this.flag = new Flag(flag);
if (!this.fn) throw new FilterNotFoundError(name);
}

_createClass(Filter, [{
Expand All @@ -238,13 +240,6 @@ var Filter = function () {
data: data
});
}
}, {
key: "fn",
get: function get() {
var fn = filters[this.name];
if (fn) return fn;
throw new FilterNotFoundError(this.name);
}
}, {
key: "args",
get: function get() {
Expand Down Expand Up @@ -424,18 +419,27 @@ var Serialiser = function () {
_createClass(Serialiser, [{
key: "yank",
value: function yank(root, data, parent) {
var node = root.filter(data);
var value = this.get(data, node.key.path, node.options);
var set = this.set.bind(this, node, parent);
var _this4 = this;

var _root$filter = root.filter(data),
children = _root$filter.children,
options = _root$filter.options,
key = _root$filter.key;

if (node.children.length) {
if (value || node.options.nullable) {
var children = this.dig(node, value);
var value = this.get(data, key.path, options);

if (node.options.extract) {
assign(parent, children);
var set = function set(child) {
return _this4.set(parent, key, child);
};

if (children.length) {
if (value || options.nullable) {
var items = this.dig(value, children);

if (options.extract) {
assign(parent, items);
} else {
set(children);
set(items);
}
}
} else {
Expand All @@ -444,20 +448,20 @@ var Serialiser = function () {
}
}, {
key: "dig",
value: function dig(node, data) {
var result = {};
var nullable = node.children.every(function (n) {
value: function dig(value, children) {
var nullable = children.every(function (n) {
return !n.options.nullable;
});
if (!data && nullable) return null;
var result = {};
if (!value && nullable) return null;

var _iterator3 = _createForOfIteratorHelper(node.children),
var _iterator3 = _createForOfIteratorHelper(children),
_step3;

try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var child = _step3.value;
this.yank(child, data, result);
this.yank(child, value, result);
}
} catch (err) {
_iterator3.e(err);
Expand Down Expand Up @@ -488,17 +492,18 @@ var Serialiser = function () {
}
}, {
key: "get",
value: function get(data, path, options) {
value: function get(item, path) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var value = path.reduce(function (acc, curr) {
return acc === null || acc === void 0 ? void 0 : acc[curr];
}, data);
}, item);
var result = options.exec ? options.exec(value) : value;
return options.nullable ? result !== null && result !== void 0 ? result : null : result;
}
}, {
key: "set",
value: function set(node, parent, value) {
parent[node.key.name] = value;
value: function set(parent, key, value) {
parent[key.name] = value;
}
}]);

Expand Down
2 changes: 1 addition & 1 deletion dist/yank.min.js

Large diffs are not rendered by default.

59 changes: 27 additions & 32 deletions dist/yank.esm.js → dist/yank.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,12 @@ class Filter {

this.filter = filter;
this.key = key;
this.params = params;
this.name = name;
this.params = params;
this.fn = filters[this.name];
this.flag = new Flag(flag);

if (!this.fn) throw new FilterNotFoundError(name)
}

apply (node, data) {
Expand All @@ -123,14 +126,6 @@ class Filter {
})
}

get fn () {
const fn = filters[this.name];

if (fn) return fn

throw new FilterNotFoundError(this.name)
}

get args () {
return this.params
?.replace(/\(([\w$_,]*)\)/g, '$1')
Expand Down Expand Up @@ -329,18 +324,18 @@ class Serialiser {
* @returns {void}
*/
yank (root, data, parent) {
const node = root.filter(data);
const value = this.get(data, node.key.path, node.options);
const set = this.set.bind(this, node, parent);
const { children, options, key } = root.filter(data);
const value = this.get(data, key.path, options);
const set = child => this.set(parent, key, child);

if (node.children.length) {
if (value || node.options.nullable) {
const children = this.dig(node, value);
if (children.length) {
if (value || options.nullable) {
const items = this.dig(value, children);

if (node.options.extract) {
assign(parent, children);
if (options.extract) {
assign(parent, items);
} else {
set(children);
set(items);
}
}
} else {
Expand All @@ -352,19 +347,19 @@ class Serialiser {
* Digs into the next level of hierarchy and yanks all properties from the
* data source via the AST schema node at that level.
*
* @param {Node} node - Current level node.
* @param {object} data - Data to extract properties from.
* @param {object} value - Data to extract properties from.
* @param {Array.<Node>} children - Child nodes.
*
* @returns {object} - Yanked properties.
*/
dig (node, data) {
dig (value, children) {
const nullable = children.every(n => !n.options.nullable);
const result = {};
const nullable = node.children.every(n => !n.options.nullable);

if (!data && nullable) return null
if (!value && nullable) return null

for (const child of node.children) {
this.yank(child, data, result);
for (const child of children) {
this.yank(child, value, result);
}

return result
Expand All @@ -387,14 +382,14 @@ class Serialiser {
/**
* Similar to Lodash.get, this retrieves properties at the given path.
*
* @param {object} data - Data to obtain the value from.
* @param {object} item - Data to obtain the value from.
* @param {Array.<string>} path - Segments of a path to the value.
* @param {object} options - Configuration for retrieving the value.
* @param {object} [options] - Configuration for retrieving the value.
*
* @returns {any | null} - Value at the given path, if it exists.
*/
get (data, path, options) {
const value = path.reduce((acc, curr) => acc?.[curr], data);
get (item, path, options = {}) {
const value = path.reduce((acc, curr) => acc?.[curr], item);
const result = options.exec
? options.exec(value)
: value;
Expand All @@ -407,14 +402,14 @@ class Serialiser {
/**
* Sets a vaLue on the given parent value.
*
* @param {Node} node - Current level node.
* @param {Node} parent - Parent node value.
* @param {Key} key - Key instance on the current Node.
* @param {any} value - Value to set onto the parent node.
*
* @returns {void}
*/
set (node, parent, value) {
parent[node.key.name] = value;
set (parent, key, value) {
parent[key.name] = value;
}

result = {}
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"build": "rollup -c",
"lint": "eslint ./src/*.js"
},
"main": "./index.js",
"exports": {
".": "./index.js",
"./configure": "./src/configure.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/marziply/yankee-doodle.git"
Expand All @@ -29,7 +34,6 @@
"node": ">=14.x",
"browsers": "> 0.25%"
},
"main": "./index.js",
"homepage": "https://github.com/marziply/yankee-doodle#readme",
"license": "MIT",
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ export default {
input: './index.js',
output: [
{
file: 'dist/yank.esm.js',
file: 'dist/yank.mjs',
format: 'esm',
exports: 'default',
},
{
file: 'dist/yank.js',
file: 'dist/yank.cjs',
format: 'cjs',
exports: 'default',
plugins: [
Expand Down
20 changes: 20 additions & 0 deletions src/configure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { flags, tokens } from './tokens.js'

const { assign } = Object

/**
* Configures the flags/tokens used by the parser and serialiser.
*
* @param {object} [options] - Flag/token config.
*
* @returns {object} - Configured options.
*/
export function configure (options = {}) {
assign(flags, options.flags ?? {})
assign(tokens, options.tokens ?? {})

return {
flags,
tokens
}
}
2 changes: 0 additions & 2 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export default class Node {
return this
}

self = () => this

children = []

options = {
Expand Down
13 changes: 13 additions & 0 deletions tests/node.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { jest } from '@jest/globals'
import Filter from '../src/filter.js'
import Property from '../src/property.js'
import Node from '../src/node.js'

describe('src/node', () => {
describe('constructor', () => {
it('should set token, shift, name, args, key, and filters', () => {
const node = new Node()

})
})
})

0 comments on commit 1a8be0c

Please sign in to comment.