Prechádzať zdrojové kódy

[extract]: add function and test

mightyplow 7 rokov pred
rodič
commit
88ecd259ab

+ 1 - 0
src/index.js

@@ -30,6 +30,7 @@ export {
 
 export {
     defaults,
+    extract,
     filter,
     fromArray,
     prop,

+ 18 - 0
src/object/__tests__/extract.test.js

@@ -0,0 +1,18 @@
+import extract from '../extract';
+
+export default test => {
+    test.ok(typeof extract === 'function', 'extract should be a function');
+
+    const input = {
+        a: 1,
+        b: 2,
+        c: 3
+    };
+
+    const extracted = extract('a', 'c')(input);
+    test.ok(extracted.hasOwnProperty('a'), 'result should have property "a"');
+    test.ok(extracted.hasOwnProperty('c'), 'result should have property "c"');
+    test.ok(!extracted.hasOwnProperty('b'), 'result should not have property "b"');
+
+    test.done();
+};

+ 8 - 0
src/object/extract.js

@@ -0,0 +1,8 @@
+const extract = (...props) => (obj) => {
+    return props.reduce((extracted, key) => {
+        extracted[key] = obj[key];
+        return extracted;
+    }, {});
+};
+
+export default extract;

+ 1 - 0
src/object/index.js

@@ -2,6 +2,7 @@
  * @namespace object
  */
 export { default as defaults } from './defaults';
+export { default as extract } from './extract';
 export { default as fromArray } from './fromArray';
 export { default as filter } from './filter';
 export { default as prop } from './prop';