From 82eb3e59a75e51b520a60576998dcc97917666b1 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 18 Jan 2018 17:40:42 +0200 Subject: [PATCH] Updated select to get --- snippets/get.md | 21 +++++++++++++++++++++ snippets/select.md | 16 ---------------- test/get/get.js | 9 +++++++++ test/get/get.test.js | 15 +++++++++++++++ test/select/select.js | 3 --- test/select/select.test.js | 15 --------------- 6 files changed, 45 insertions(+), 34 deletions(-) create mode 100644 snippets/get.md delete mode 100644 snippets/select.md create mode 100644 test/get/get.js create mode 100644 test/get/get.test.js delete mode 100644 test/select/select.js delete mode 100644 test/select/select.test.js diff --git a/snippets/get.md b/snippets/get.md new file mode 100644 index 000000000..48a485cd6 --- /dev/null +++ b/snippets/get.md @@ -0,0 +1,21 @@ +### get + +Retrieve a set of properties indicated by the given selectors from an object. + +Use `Array.map()` for each selector, `String.replace()` to replace square brackets with dots, `String.split('.')` to split each selector, `Array.filter()` to remove empty values and `Array.reduce()` to get the value indicated by it. + +```js +const get = (from, ...selectors) => + [...selectors].map(s => + s + .replace(/\[([^\[\]]*)\]/g, '.$1.') + .split('.') + .filter(t => t !== '') + .reduce((prev, cur) => prev && prev[cur], from) + ); +``` + +```js +const obj = { selector: { to: { val: 'val to select' } }, target: [1, 2, {a: 'test'}] }; +get(obj, 'selector.to.val', 'target[0]', 'target[2].a'); // ['val to select', 1, 'test'] +``` diff --git a/snippets/select.md b/snippets/select.md deleted file mode 100644 index 07c95cb0f..000000000 --- a/snippets/select.md +++ /dev/null @@ -1,16 +0,0 @@ -### select - -Retrieve a set of properties indicated by the given selectors from an object. - -Use `Array.map()` for each selector, `String.split('.')` to split each selector and `Array.reduce()` to get the value indicated by it. - -```js -const select = (from, ...selectors) => - [...selectors].map(s => s.split('.').reduce((prev, cur) => prev && prev[cur], from)); -``` - -```js -const obj = { selector: { to: { val: 'val to select' } } }; -select(obj, 'selector.to.val'); // ['val to select'] -select(obj, 'selector.to.val', 'selector.to'); // ['val to select', { val: 'val to select' }] -``` diff --git a/test/get/get.js b/test/get/get.js new file mode 100644 index 000000000..a4f80d774 --- /dev/null +++ b/test/get/get.js @@ -0,0 +1,9 @@ +const get = (from, ...selectors) => + [...selectors].map(s => + s + .replace(/\[([^\[\]]*)\]/g, '.$1.') + .split('.') + .filter(t => t !== '') + .reduce((prev, cur) => prev && prev[cur], from) + ); + module.exports = get diff --git a/test/get/get.test.js b/test/get/get.test.js new file mode 100644 index 000000000..fbe638391 --- /dev/null +++ b/test/get/get.test.js @@ -0,0 +1,15 @@ +const test = require('tape'); +const get = require('./get.js'); + +test('Testing get', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof get === 'function', 'get is a Function'); + const obj = { selector: { to: { val: 'val to get' } } }; + t.deepEqual(get(obj, 'selector.to.val'), ['val to get'], "Retrieve a property indicated by the selector from an object."); + //t.deepEqual(get(args..), 'Expected'); + //t.equal(get(args..), 'Expected'); + //t.false(get(args..), 'Expected'); + //t.throws(get(args..), 'Expected'); + t.end(); +}); diff --git a/test/select/select.js b/test/select/select.js deleted file mode 100644 index 58ef08f84..000000000 --- a/test/select/select.js +++ /dev/null @@ -1,3 +0,0 @@ -const select = (from, ...selectors) => -[...selectors].map(s => s.split('.').reduce((prev, cur) => prev && prev[cur], from)); - module.exports = select \ No newline at end of file diff --git a/test/select/select.test.js b/test/select/select.test.js deleted file mode 100644 index eb5a1b3a2..000000000 --- a/test/select/select.test.js +++ /dev/null @@ -1,15 +0,0 @@ -const test = require('tape'); -const select = require('./select.js'); - -test('Testing select', (t) => { - //For more information on all the methods supported by tape - //Please go to https://github.com/substack/tape - t.true(typeof select === 'function', 'select is a Function'); - const obj = { selector: { to: { val: 'val to select' } } }; - t.deepEqual(select(obj, 'selector.to.val'), ['val to select'], "Retrieve a property indicated by the selector from an object."); - //t.deepEqual(select(args..), 'Expected'); - //t.equal(select(args..), 'Expected'); - //t.false(select(args..), 'Expected'); - //t.throws(select(args..), 'Expected'); - t.end(); -});