Adapter
call
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = (key, ...args) => context => context[key](...args); + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Adapter
call
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = (key, ...args) => context => context[key](...args);Promise.resolve([1, 2, 3]) .then(call('map', x => 2 * x)) .then(console.log); //[ 2, 4, 6 ] @@ -1059,6 +1059,13 @@ console.log<isNumber
Checks if the given argument is a number.
Use
typeofto check if a value is classified as a number primitive.const isNumber = val => typeof val === 'number';isNumber('1'); // false isNumber(1); // true +isObject
Returns a boolean determining if the passed value is an object or not.
Uses the
Objectconstructor to create an object wrapper for the given value. If the value isnullorundefined, create and return an empty object. Οtherwise, return an object of a type that corresponds to the given value.const isObject = obj => obj === Object(obj); +isObject([1, 2, 3, 4]); // true +isObject([]); // true +isObject(['Hello!']); // true +isObject({ a: 1 }); // true +isObject({}); // true +isObject(true); // falseisPrimitive
Returns a boolean determining if the passed value is primitive or not.
Use
Array.includes()on an array of type strings which are not primitive, supplying the type usingtypeof. Sincetypeof nullevaluates to'object', it needs to be directly compared.const isPrimitive = val => !['object', 'function'].includes(typeof val) || val === null;isPrimitive(null); // true isPrimitive(50); // true @@ -1171,6 +1178,7 @@ Logs: { + const newPost = { "userId": 1, "id": 1337, diff --git a/snippets/httpPost.md b/snippets/httpPost.md index 55d29a095..e9e6d1e70 100644 --- a/snippets/httpPost.md +++ b/snippets/httpPost.md @@ -30,6 +30,7 @@ const httpPost = (url, callback, data = null, err = console.error) => { + const newPost = { "userId": 1, "id": 1337, diff --git a/snippets/isObject.md b/snippets/isObject.md index c4cc5ffa8..eb8aee625 100644 --- a/snippets/isObject.md +++ b/snippets/isObject.md @@ -13,7 +13,7 @@ const isObject = obj => obj === Object(obj); isObject([1, 2, 3, 4]); // true isObject([]); // true isObject(['Hello!']); // true -isObject({ a:1 }); // true +isObject({ a: 1 }); // true isObject({}); // true isObject(true); // false ```