ES2015
Babel
Objects
Data Types
Joeun's implementation of the pick function in the 'underscore' library. I have extended the library with a new function with a clever name. :) Called 'flick' which is the inverse of 'pick'. Some object/booger picking operations. lol
var _ = {}; // Joeun's Pick implementation (GREAT JOB!) _.pick = (target, ...keys) => { if (typeof keys[0] == 'function') { var predicate = keys[0]; keys = Object.keys(target); return keys.reduce((obj, key) => { return predicate(target[key], key, target) ? (obj[key] = target[key], obj) : obj; }, {}) } return keys.reduce((obj, key) => { return obj[key] = target[key], obj; }, {}); }; // Robert.Cutright's implementation of the inverse of pick (called flick); // In other words, you supply the keys you want to throw away. _.flick = (target, ...keys) => { if (typeof keys[0] == 'function') { var predicate = keys[0]; keys = Object.keys(target); return keys.reduce((obj, key) => { return predicate(target[key], key, target) ? obj : (obj[key] = target[key], obj); }, {}) } var obj = Object.assign({}, target); Object.keys(obj).filter(key => keys.includes(key) ? delete obj[key] : obj[key]); return obj; };
- var _ = {};
- // Joeun's Pick implementation (GREAT JOB!)
- _.pick = (target, ...keys) => {
- if (typeof keys[0] == 'function') {
- var predicate = keys[0];
- keys = Object.keys(target);
- return keys.reduce((obj, key) => {
- return predicate(target[key], key, target) ? (obj[key] = target[key], obj) : obj;
- }, {})
- }
- return keys.reduce((obj, key) => {
- return obj[key] = target[key], obj;
- }, {});
};- };
- // Robert.Cutright's implementation of the inverse of pick (called flick);
- // In other words, you supply the keys you want to throw away.
- _.flick = (target, ...keys) => {
- if (typeof keys[0] == 'function') {
- var predicate = keys[0];
- keys = Object.keys(target);
- return keys.reduce((obj, key) => {
- return predicate(target[key], key, target) ? obj : (obj[key] = target[key], obj);
- }, {})
- }
- var obj = Object.assign({}, target);
- Object.keys(obj).filter(key => keys.includes(key) ? delete obj[key] : obj[key]);
- return obj;
- };
var result; var testObj = {name: 'moe', age: 50, userid: 'moe1'}; // _.pick Tests result = _.pick(testObj, 'name', 'age'); Test.assertSimilar(result, {name: 'moe', age: 50}); result = _.pick(testObj, function(value, key, object) { return typeof value == 'number'; }); Test.assertSimilar(result, {age: 50}); // _.flick Tests result = _.flick(testObj, 'name', 'age'); Test.assertSimilar(result, {userid: 'moe1'}); result = _.flick(testObj, function(value, key, object) { return typeof value == 'number'; }); Test.assertSimilar(result, {name: 'moe', userid: 'moe1'});
- var result;
result = _.pick({name: 'moe', age: 50, userid: 'moe1'}, 'name', 'age');- var testObj = {name: 'moe', age: 50, userid: 'moe1'};
- // _.pick Tests
- result = _.pick(testObj, 'name', 'age');
- Test.assertSimilar(result, {name: 'moe', age: 50});
result = _.pick({name: 'moe', age: 50, userid: 'moe1'}, function(value, key, object) {- result = _.pick(testObj, function(value, key, object) {
- return typeof value == 'number';
- });
- Test.assertSimilar(result, {age: 50});
- // _.flick Tests
- result = _.flick(testObj, 'name', 'age');
- Test.assertSimilar(result, {userid: 'moe1'});
- result = _.flick(testObj, function(value, key, object) {
- return typeof value == 'number';
- });
Test.assertSimilar(result, {age: 50});- Test.assertSimilar(result, {name: 'moe', userid: 'moe1'});