@straylightagency/utils 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +27 -0
- package/readme.MD +0 -0
- package/src/array/index.js +9 -0
- package/src/array/initial.js +10 -0
- package/src/array/last.js +9 -0
- package/src/array/remove.js +14 -0
- package/src/date/getWeekNumber.js +12 -0
- package/src/date/index.js +5 -0
- package/src/dom/addEventListeners.js +13 -0
- package/src/dom/closestElement.js +30 -0
- package/src/dom/delegate.js +17 -0
- package/src/dom/getFieldsValues.js +15 -0
- package/src/dom/index.js +19 -0
- package/src/dom/onEachElements.js +21 -0
- package/src/dom/unlessElementExists.js +25 -0
- package/src/dom/whenReady.js +7 -0
- package/src/dom/whenResize.js +16 -0
- package/src/index.js +53 -0
- package/src/misc/clipboard.js +23 -0
- package/src/misc/debounce.js +25 -0
- package/src/misc/index.js +15 -0
- package/src/misc/is.js +26 -0
- package/src/misc/nextTick.js +11 -0
- package/src/misc/throttle.js +28 -0
- package/src/misc/wait.js +25 -0
- package/src/object/filter.js +9 -0
- package/src/object/index.js +9 -0
- package/src/object/map.js +11 -0
- package/src/object/only.js +14 -0
- package/src/string/capitalize.js +7 -0
- package/src/string/index.js +5 -0
- package/src/string/serialize.js +7 -0
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@straylightagency/utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Some JavaScript utilities functions",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"src"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
12
|
+
},
|
|
13
|
+
"private": false,
|
|
14
|
+
"dependencies": {},
|
|
15
|
+
"devDependencies": {},
|
|
16
|
+
"keywords": [],
|
|
17
|
+
"author": "Anthony Pauwels (anthony@straylightagency.be)",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/straylightagency/js-utils.git"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/straylightagency/js-utils/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/straylightagency/js-utils#readme"
|
|
27
|
+
}
|
package/readme.MD
ADDED
|
File without changes
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param date {Date}
|
|
3
|
+
* @returns {number}
|
|
4
|
+
*/
|
|
5
|
+
export default function getWeekNumber(date) {
|
|
6
|
+
date.setHours(0, 0, 0, 0);
|
|
7
|
+
date.setDate( date.getDate() + 3 - ( date.getDay() + 6 ) % 7 );
|
|
8
|
+
|
|
9
|
+
const first_week = new Date( date.getUTCFullYear(), 0, 4 );
|
|
10
|
+
|
|
11
|
+
return 1 + Math.round(( ( date.getTime() - first_week.getTime() ) / 86400000 - 3 + ( first_week.getDay() + 6 ) % 7 ) / 7 );
|
|
12
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param element {HTMLElement}
|
|
3
|
+
* @param listeners {{}}
|
|
4
|
+
*/
|
|
5
|
+
export default function addEventListeners(element, listeners) {
|
|
6
|
+
Object.fromEntries(
|
|
7
|
+
Object.entries( listeners ).map( ([event, callbackFn]) => {
|
|
8
|
+
element.addEventListener( event, callbackFn );
|
|
9
|
+
|
|
10
|
+
return [ event, () => element.removeEventListener( event, callbackFn ) ];
|
|
11
|
+
} )
|
|
12
|
+
);
|
|
13
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param element {HTMLElement}
|
|
3
|
+
* @param selector {String}
|
|
4
|
+
* @returns {HTMLAnchorElement|HTMLElement|HTMLAreaElement|HTMLAudioElement|HTMLBaseElement|HTMLQuoteElement|HTMLBodyElement|HTMLBRElement|HTMLButtonElement|HTMLCanvasElement|HTMLTableCaptionElement|HTMLTableColElement|HTMLDataElement|HTMLDataListElement|HTMLModElement|HTMLDetailsElement|HTMLDialogElement|HTMLDivElement|HTMLDListElement|HTMLEmbedElement|HTMLFieldSetElement|HTMLFormElement|HTMLHeadingElement|HTMLHeadElement|HTMLHRElement|HTMLHtmlElement|HTMLIFrameElement|HTMLImageElement|HTMLInputElement|HTMLLabelElement|HTMLLegendElement|HTMLLIElement|HTMLLinkElement|HTMLMapElement|HTMLMenuElement|HTMLMetaElement|HTMLMeterElement|HTMLObjectElement|HTMLOListElement|HTMLOptGroupElement|HTMLOptionElement|HTMLOutputElement|HTMLParagraphElement|HTMLPictureElement|HTMLPreElement|HTMLProgressElement|HTMLScriptElement|HTMLSelectElement|HTMLSlotElement|HTMLSourceElement|HTMLSpanElement|HTMLStyleElement|HTMLTableElement|HTMLTableSectionElement|HTMLTableCellElement|HTMLTemplateElement|HTMLTextAreaElement|HTMLTimeElement|HTMLTitleElement|HTMLTableRowElement|HTMLTrackElement|HTMLUListElement|HTMLVideoElement|*}
|
|
5
|
+
*/
|
|
6
|
+
export default function closestElement(element, selector) {
|
|
7
|
+
const recurse = parent => {
|
|
8
|
+
parent = parent.parentNode;
|
|
9
|
+
|
|
10
|
+
if ( !parent ) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const sibling = parent.nextElementSibling;
|
|
15
|
+
|
|
16
|
+
if ( sibling ) {
|
|
17
|
+
const closestSibling = sibling.closest( selector );
|
|
18
|
+
|
|
19
|
+
if ( closestSibling ) {
|
|
20
|
+
return closestSibling;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return recurse( sibling );
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return recurse( parent );
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
return recurse( element );
|
|
30
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param selector {String}
|
|
3
|
+
* @param event {String}
|
|
4
|
+
* @param handler {Function}
|
|
5
|
+
* @returns {function(): any}
|
|
6
|
+
*/
|
|
7
|
+
export default function delegate(selector, event, handler) {
|
|
8
|
+
const callback = function (event) {
|
|
9
|
+
const matchingElement = event.target.closest( selector );
|
|
10
|
+
|
|
11
|
+
return matchingElement !== null ? handler.call( matchingElement, event ) : false;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
Element.prototype.addEventListener.call( document, event, callback, true );
|
|
15
|
+
|
|
16
|
+
return () => Element.prototype.removeEventListener.call( document, event, callback, true );
|
|
17
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param form {HTMLFormElement}
|
|
3
|
+
* @returns {{[p: string]: *}}
|
|
4
|
+
*/
|
|
5
|
+
export default function (form) {
|
|
6
|
+
return Object.fromEntries(
|
|
7
|
+
[ ...form.elements ]
|
|
8
|
+
.map( item => {
|
|
9
|
+
const value = item.type === 'checkbox' ? item.checked : item.value;
|
|
10
|
+
|
|
11
|
+
return [ item.name, value ];
|
|
12
|
+
} )
|
|
13
|
+
.filter( ( [ key ] ) => key.length > 0 )
|
|
14
|
+
);
|
|
15
|
+
}
|
package/src/dom/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import addEventListeners from "./addEventListeners.js";
|
|
2
|
+
import closestElement from "./closestElement.js";
|
|
3
|
+
import delegate from "./delegate.js";
|
|
4
|
+
import getFieldsValues from "./getFieldsValues.js";
|
|
5
|
+
import onEachElements from "./onEachElements.js";
|
|
6
|
+
import unlessElementExists from "./unlessElementExists.js";
|
|
7
|
+
import whenReady from "./whenReady.js";
|
|
8
|
+
import whenResize from "./whenResize.js";
|
|
9
|
+
|
|
10
|
+
export default {
|
|
11
|
+
addEventListeners,
|
|
12
|
+
closestElement,
|
|
13
|
+
delegate,
|
|
14
|
+
getFieldsValues,
|
|
15
|
+
onEachElements,
|
|
16
|
+
unlessElementExists,
|
|
17
|
+
whenReady,
|
|
18
|
+
whenResize,
|
|
19
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param selectorOrParent {String|HTMLElement}
|
|
3
|
+
* @param callbackOrSelector {Function|HTMLElement}
|
|
4
|
+
* @param callbackOrNull {Function|undefined}
|
|
5
|
+
* @returns {*}
|
|
6
|
+
*/
|
|
7
|
+
export default function onEachElements(selectorOrParent, callbackOrSelector, callbackOrNull) {
|
|
8
|
+
if ( typeof selectorOrParent === 'string' && typeof callbackOrSelector === 'function' && typeof callbackOrNull !== 'function' ) {
|
|
9
|
+
const elements = document.querySelectorAll( selectorOrParent );
|
|
10
|
+
|
|
11
|
+
elements.forEach( callbackOrSelector );
|
|
12
|
+
|
|
13
|
+
return elements;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const elements = selectorOrParent.querySelectorAll( callbackOrSelector );
|
|
17
|
+
|
|
18
|
+
elements.forEach( callbackOrNull );
|
|
19
|
+
|
|
20
|
+
return elements;
|
|
21
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param selectorOrParent {String|HTMLElement}
|
|
3
|
+
* @param callbackOrSelector {Function|HTMLElement}
|
|
4
|
+
* @param callbackOrNull {Function|undefined}
|
|
5
|
+
* @returns {*}
|
|
6
|
+
*/
|
|
7
|
+
export default function unlessElementExists(selectorOrParent, callbackOrSelector, callbackOrNull = null) {
|
|
8
|
+
if ( typeof selectorOrParent === 'string' && typeof callbackOrSelector === 'function' && typeof callbackOrNull !== 'function' ) {
|
|
9
|
+
const element = document.querySelector( selectorOrParent );
|
|
10
|
+
|
|
11
|
+
if ( element ) {
|
|
12
|
+
callbackOrSelector( element );
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return element;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const element = selectorOrParent.querySelector( callbackOrSelector );
|
|
19
|
+
|
|
20
|
+
if ( element && typeof callbackOrNull === 'function' ) {
|
|
21
|
+
callbackOrNull( element );
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return element;
|
|
25
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param callbackFn {Function}
|
|
3
|
+
* @param init
|
|
4
|
+
*/
|
|
5
|
+
export default function whenResize(callbackFn, init = true) {
|
|
6
|
+
let ww = window.innerWidth, wh = window.innerHeight;
|
|
7
|
+
|
|
8
|
+
window.addEventListener( 'resize', () => {
|
|
9
|
+
ww = window.innerWidth;
|
|
10
|
+
wh = window.innerHeight;
|
|
11
|
+
|
|
12
|
+
callbackFn( { ww, wh } );
|
|
13
|
+
} );
|
|
14
|
+
|
|
15
|
+
init ? callbackFn( { ww, wh } ) : null;
|
|
16
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import capitalize from "./string/capitalize.js";
|
|
2
|
+
import serialize from "./string/serialize.js";
|
|
3
|
+
import filter from "./object/filter.js";
|
|
4
|
+
import map from "./object/map.js";
|
|
5
|
+
import only from "./object/only.js";
|
|
6
|
+
import clipboard from "./misc/clipboard.js";
|
|
7
|
+
import debounce from "./misc/debounce.js";
|
|
8
|
+
import nextTick from "./misc/nextTick.js";
|
|
9
|
+
import throttle from "./misc/throttle.js";
|
|
10
|
+
import {wait, seconds, milliseconds} from "./misc/wait.js";
|
|
11
|
+
import {isClass, isObject, isPlainObject} from "./misc/is.js";
|
|
12
|
+
import addEventListeners from "./dom/addEventListeners.js";
|
|
13
|
+
import closestElement from "./dom/closestElement.js";
|
|
14
|
+
import delegate from "./dom/delegate.js";
|
|
15
|
+
import getFieldsValues from "./dom/getFieldsValues.js";
|
|
16
|
+
import onEachElements from "./dom/onEachElements.js";
|
|
17
|
+
import unlessElementExists from "./dom/unlessElementExists.js";
|
|
18
|
+
import whenReady from "./dom/whenReady.js";
|
|
19
|
+
import whenResize from "./dom/whenResize.js";
|
|
20
|
+
import getWeekNumber from "./date/getWeekNumber.js";
|
|
21
|
+
import initial from "./array/initial.js";
|
|
22
|
+
import last from "./array/last.js";
|
|
23
|
+
import remove from "./array/remove.js";
|
|
24
|
+
|
|
25
|
+
export default {
|
|
26
|
+
initial,
|
|
27
|
+
last,
|
|
28
|
+
remove,
|
|
29
|
+
getWeekNumber,
|
|
30
|
+
addEventListeners,
|
|
31
|
+
closestElement,
|
|
32
|
+
delegate,
|
|
33
|
+
getFieldsValues,
|
|
34
|
+
onEachElements,
|
|
35
|
+
unlessElementExists,
|
|
36
|
+
whenReady,
|
|
37
|
+
whenResize,
|
|
38
|
+
clipboard,
|
|
39
|
+
debounce,
|
|
40
|
+
nextTick,
|
|
41
|
+
throttle,
|
|
42
|
+
wait,
|
|
43
|
+
seconds,
|
|
44
|
+
milliseconds,
|
|
45
|
+
isClass,
|
|
46
|
+
isObject,
|
|
47
|
+
isPlainObject,
|
|
48
|
+
filter,
|
|
49
|
+
map,
|
|
50
|
+
only,
|
|
51
|
+
capitalize,
|
|
52
|
+
serialize,
|
|
53
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param text {String}
|
|
3
|
+
* @returns {Promise<void>}
|
|
4
|
+
*/
|
|
5
|
+
export default async function clipboard(text) {
|
|
6
|
+
if ( typeof ( navigator.clipboard ) == 'undefined' ) {
|
|
7
|
+
await new Promise( () => {
|
|
8
|
+
const textArea = document.createElement("textarea");
|
|
9
|
+
textArea.value = text;
|
|
10
|
+
textArea.style.position = "fixed"; //avoid scrolling to bottom
|
|
11
|
+
document.body.appendChild( textArea );
|
|
12
|
+
textArea.focus();
|
|
13
|
+
textArea.select();
|
|
14
|
+
|
|
15
|
+
document.execCommand('copy');
|
|
16
|
+
|
|
17
|
+
document.body.removeChild( textArea );
|
|
18
|
+
} );
|
|
19
|
+
|
|
20
|
+
} else {
|
|
21
|
+
await navigator.clipboard.writeText( text );
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Execute callback once if called many times during a defined delay
|
|
3
|
+
*
|
|
4
|
+
* @param callbackFn {Function}
|
|
5
|
+
* @param delay {Number}
|
|
6
|
+
* @param immediate {Boolean}
|
|
7
|
+
* @returns {(function(): void)|*}
|
|
8
|
+
*/
|
|
9
|
+
export default function debounce(callbackFn, delay, immediate = false) {
|
|
10
|
+
let timer;
|
|
11
|
+
|
|
12
|
+
return (...args) => {
|
|
13
|
+
const now = immediate && !timer;
|
|
14
|
+
|
|
15
|
+
clearTimeout( timer );
|
|
16
|
+
|
|
17
|
+
timer = setTimeout( () => {
|
|
18
|
+
timer = undefined;
|
|
19
|
+
|
|
20
|
+
if ( !immediate ) callbackFn.apply( this, args );
|
|
21
|
+
}, delay );
|
|
22
|
+
|
|
23
|
+
if ( now ) callbackFn.apply( this, args );
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import clipboard from "./clipboard.js";
|
|
2
|
+
import debounce from "./debounce.js";
|
|
3
|
+
import nextTick from "./nextTick.js";
|
|
4
|
+
import throttle from "./throttle.js";
|
|
5
|
+
import {wait, seconds, milliseconds} from "./wait.js";
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
clipboard,
|
|
9
|
+
debounce,
|
|
10
|
+
nextTick,
|
|
11
|
+
throttle,
|
|
12
|
+
wait,
|
|
13
|
+
seconds,
|
|
14
|
+
milliseconds,
|
|
15
|
+
};
|
package/src/misc/is.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const toString = Object.prototype.toString;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param obj
|
|
5
|
+
* @returns {boolean}
|
|
6
|
+
*/
|
|
7
|
+
export function isObject(obj) {
|
|
8
|
+
return obj !== null && typeof obj === 'object'
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param obj
|
|
13
|
+
* @param className
|
|
14
|
+
* @returns {boolean}
|
|
15
|
+
*/
|
|
16
|
+
export function isClass(obj, className) {
|
|
17
|
+
return isObject( obj ) && toString.call( obj ) === '[object ' + className + ']';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param obj
|
|
22
|
+
* @returns {boolean}
|
|
23
|
+
*/
|
|
24
|
+
export function isPlainObject(obj) {
|
|
25
|
+
return isClass( obj, 'Object' );
|
|
26
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param callbackFn {Function}
|
|
3
|
+
* @returns {Promise<unknown>}
|
|
4
|
+
*/
|
|
5
|
+
export default async function nextTick(callbackFn) {
|
|
6
|
+
return new Promise(
|
|
7
|
+
async resolve => requestIdleCallback( () =>
|
|
8
|
+
setTimeout( async () => await resolve( await callbackFn() ), 0 )
|
|
9
|
+
)
|
|
10
|
+
);
|
|
11
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Limit the number of times a callback gets called in a certain time period
|
|
3
|
+
*
|
|
4
|
+
* @param callbackFn {Function}
|
|
5
|
+
* @param delay {Number}
|
|
6
|
+
* @returns {(function(): void)|*}
|
|
7
|
+
*/
|
|
8
|
+
export default function throttle(callbackFn, delay) {
|
|
9
|
+
let last;
|
|
10
|
+
let timer;
|
|
11
|
+
|
|
12
|
+
return (...args) => {
|
|
13
|
+
let context = this;
|
|
14
|
+
let now = +new Date();
|
|
15
|
+
|
|
16
|
+
if ( last && now < last + delay ) {
|
|
17
|
+
clearTimeout( timer );
|
|
18
|
+
|
|
19
|
+
timer = setTimeout( () => {
|
|
20
|
+
last = now;
|
|
21
|
+
callbackFn.apply( context, args );
|
|
22
|
+
}, delay );
|
|
23
|
+
} else {
|
|
24
|
+
last = now;
|
|
25
|
+
callbackFn.apply( context, args );
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
package/src/misc/wait.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param timeMs {Number}
|
|
3
|
+
* @returns {Promise<unknown>}
|
|
4
|
+
*/
|
|
5
|
+
export function wait(timeMs) {
|
|
6
|
+
return new Promise( resolve => setTimeout( resolve, timeMs ) );
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param time {Number}
|
|
11
|
+
* @returns {Promise<*>}
|
|
12
|
+
*/
|
|
13
|
+
export function seconds(time) {
|
|
14
|
+
return wait( time * 1000 );
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param timeMs {Number}
|
|
19
|
+
* @returns {Promise<*>}
|
|
20
|
+
*/
|
|
21
|
+
export function milliseconds(timeMs) {
|
|
22
|
+
return wait( timeMs );
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default wait;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param object {{}}
|
|
3
|
+
* @param callbackFn {Function}
|
|
4
|
+
* @returns {{[p: string]: unknown}}
|
|
5
|
+
*/
|
|
6
|
+
export default function filter(object, callbackFn) {
|
|
7
|
+
return Object.fromEntries( Object.entries( object ).
|
|
8
|
+
filter( ( [ key, value ] ) => callbackFn( value ) ) );
|
|
9
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param object {{}}
|
|
3
|
+
* @param callbackFn {Function}
|
|
4
|
+
* @returns {{}}
|
|
5
|
+
*/
|
|
6
|
+
export default function map(object, callbackFn) {
|
|
7
|
+
return Object.keys( object ).reduce( ( result, key ) => {
|
|
8
|
+
result[ key ] = callbackFn( object [ key ] );
|
|
9
|
+
return result;
|
|
10
|
+
}, {});
|
|
11
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param object {{}}
|
|
3
|
+
* @param keys {[]}
|
|
4
|
+
* @returns {{}}
|
|
5
|
+
*/
|
|
6
|
+
export default function only(object, keys) {
|
|
7
|
+
return Object.entries( object ).reduce( (acc, [key, value]) => {
|
|
8
|
+
if ( keys.indexOf( key ) !== -1 ) {
|
|
9
|
+
acc[ key ] = value;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return acc;
|
|
13
|
+
}, {} );
|
|
14
|
+
}
|