From 0be16616ae043af0c8012583570cd65488faacca Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Wed, 17 Jan 2018 19:40:40 +0000 Subject: [PATCH] Travis build: 1293 --- README.md | 185 +++++------------------------------------------- docs/index.html | 32 ++++----- snippets/is.md | 2 +- 3 files changed, 34 insertions(+), 185 deletions(-) diff --git a/README.md b/README.md index 7aca4b9d9..61bcfcf9d 100644 --- a/README.md +++ b/README.md @@ -327,27 +327,20 @@ average(1, 2, 3); View contents * [`getType`](#gettype) -* [`isArray`](#isarray) -* [`isArrayBuffer`](#isarraybuffer) +* [`is`](#is) * [`isArrayLike`](#isarraylike) * [`isBoolean`](#isboolean) * [`isFunction`](#isfunction) -* [`isMap`](#ismap) * [`isNil`](#isnil) * [`isNull`](#isnull) * [`isNumber`](#isnumber) * [`isObject`](#isobject) * [`isPrimitive`](#isprimitive) * [`isPromiseLike`](#ispromiselike) -* [`isRegExp`](#isregexp) -* [`isSet`](#isset) * [`isString`](#isstring) * [`isSymbol`](#issymbol) -* [`isTypedArray`](#istypedarray) * [`isUndefined`](#isundefined) * [`isValidJSON`](#isvalidjson) -* [`isWeakMap`](#isweakmap) -* [`isWeakSet`](#isweakset) @@ -5264,43 +5257,33 @@ getType(new Set([1, 2, 3])); // 'set'
[⬆ Back to top](#table-of-contents) -### isArray +### is -Checks if the given argument is an array. +Checks if the provided value is of the specified type (doesn't work with literals). -Use `Array.isArray()` to check if a value is classified as an array. +Use the `instanceof` operator to check if the provided value is of the specified `type`. ```js -const isArray = val => Array.isArray(val); +const is = (type, val) => val instanceof type; ```
Examples ```js -isArray([1]); // true -``` - -
- -
[⬆ Back to top](#table-of-contents) - - -### isArrayBuffer - -Checks if value is classified as a ArrayBuffer object. - -Use the `instanceof`operator to check if the provided value is a `ArrayBuffer` object. - -```js -const isArrayBuffer = val => val instanceof ArrayBuffer; -``` - -
-Examples - -```js -isArrayBuffer(new ArrayBuffer()); // true +is(Array, [1]); // true +is(ArrayBuffer, new ArrayBuffer()); // true +is(Map, new Map()); // true +is(RegExp, /./g); // true +is(Set, new Set()); // true +is(WeakMap, new WeakMap()); // true +is(WeakSet, new WeakSet()); // true +is(String, ''); // false +is(String, new String('')); // true +is(Number, 1); // false +is(Number, new Number(1)); // true +is(Boolean, true); // false +is(Boolean, new Boolean(true)); // true ```
@@ -5384,28 +5367,6 @@ isFunction(x => x); // true
[⬆ Back to top](#table-of-contents) -### isMap - -Checks if value is classified as a Map object. - -Use the `instanceof`operator to check if the provided value is a `Map` object. - -```js -const isMap = val => val instanceof Map; -``` - -
-Examples - -```js -isMap(new Map()); // true -``` - -
- -
[⬆ Back to top](#table-of-contents) - - ### isNil Returns `true` if the specified value is `null` or `undefined`, `false` otherwise. @@ -5562,50 +5523,6 @@ isPromiseLike({}); // false
[⬆ Back to top](#table-of-contents) -### isRegExp - -Checks if value is classified as a RegExp object. - -Use the `instanceof`operator to check if the provided value is a `RegExp` object. - -```js -const isRegExp = val => val instanceof RegExp; -``` - -
-Examples - -```js -isRegExp(/./g); // true -``` - -
- -
[⬆ Back to top](#table-of-contents) - - -### isSet - -Checks if value is classified as a Set object. - -Use the `instanceof`operator to check if the provided value is a `Set` object. - -```js -const isSet = val => val instanceof Set; -``` - -
-Examples - -```js -isSet(new Set()); // true -``` - -
- -
[⬆ Back to top](#table-of-contents) - - ### isString Checks if the given argument is a string. @@ -5650,28 +5567,6 @@ isSymbol(Symbol('x')); // true
[⬆ Back to top](#table-of-contents) -### isTypedArray - -Checks if value is classified as a TypedArray object. - -Use the `instanceof`operator to check if the provided value is a `TypedArray` object. - -```js -const isTypedArray = val => val instanceof TypedArray; -``` - -
-Examples - -```js -isTypedArray(new TypedArray()); // true -``` - -
- -
[⬆ Back to top](#table-of-contents) - - ### isUndefined Returns `true` if the specified value is `undefined`, `false` otherwise. @@ -5724,50 +5619,6 @@ isValidJSON(null); // true
[⬆ Back to top](#table-of-contents) - -### isWeakMap - -Checks if value is classified as a WeakMap object. - -Use the `instanceof`operator to check if the provided value is a `WeakMap` object. - -```js -const isWeakMap = val => val instanceof WeakMap; -``` - -
-Examples - -```js -isWeakMap(new WeakMap()); // true -``` - -
- -
[⬆ Back to top](#table-of-contents) - - -### isWeakSet - -Checks if value is classified as a WeakSet object. - -Use the `instanceof`operator to check if the provided value is a `WeakSet` object. - -```js -const isWeakSet = val => val instanceof WeakSet; -``` - -
-Examples - -```js -isWeakSet(new WeakSet()); // true -``` - -
- -
[⬆ Back to top](#table-of-contents) - --- ## 🔧 Utility diff --git a/docs/index.html b/docs/index.html index 6720fc8de..cc3507797 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 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);
+      }

logo 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 ]
@@ -1205,10 +1205,20 @@ Foo.prototype📋 Copy to clipboard

Type

getType

Returns the native type of a value.

Returns lowercased constructor name of value, "undefined" or "null" if value is undefined or null.

const getType = v =>
   v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();
 
getType(new Set([1, 2, 3])); // 'set'
-

isArray

Checks if the given argument is an array.

Use Array.isArray() to check if a value is classified as an array.

const isArray = val => Array.isArray(val);
-
isArray([1]); // true
-

isArrayBuffer

Checks if value is classified as a ArrayBuffer object.

Use the instanceofoperator to check if the provided value is a ArrayBuffer object.

const isArrayBuffer = val => val instanceof ArrayBuffer;
-
isArrayBuffer(new ArrayBuffer()); // true
+

is

Checks if the provided value is of the specified type (doesn't work with literals).

Use the instanceof operator to check if the provided value is of the specified type.

const is = (type, val) => val instanceof type;
+
is(Array, [1]); // true
+is(ArrayBuffer, new ArrayBuffer()); // true
+is(Map, new Map()); // true
+is(RegExp, /./g); // true
+is(Set, new Set()); // true
+is(WeakMap, new WeakMap()); // true
+is(WeakSet, new WeakSet()); // true
+is(String, ''); // false
+is(String, new String('')); // true
+is(Number, 1); // false
+is(Number, new Number(1)); // true
+is(Boolean, true); // false
+is(Boolean, new Boolean(true)); // true
 

isArrayLike

Checks if the provided argument is array-like (i.e. is iterable).

Use the spread operator (...) to check if the provided argument is iterable inside a try... catch block and the comma operator (,) to return the appropriate value.

const isArrayLike = val => {
   try {
     return [...val], true;
@@ -1225,8 +1235,6 @@ Foo.prototype📋 Copy to clipboard

isFunction

Checks if the given argument is a function.

Use typeof to check if a value is classified as a function primitive.

const isFunction = val => typeof val === 'function';
 
isFunction('x'); // false
 isFunction(x => x); // true
-

isMap

Checks if value is classified as a Map object.

Use the instanceofoperator to check if the provided value is a Map object.

const isMap = val => val instanceof Map;
-
isMap(new Map()); // true
 

isNil

Returns true if the specified value is null or undefined, false otherwise.

Use the strict equality operator to check if the value and of val are equal to null or undefined.

const isNil = val => val === undefined || val === null;
 
isNil(null); // true
 isNil(undefined); // true
@@ -1260,16 +1268,10 @@ Foo.prototype// true
 isPromiseLike(null); // false
 isPromiseLike({}); // false
-

isRegExp

Checks if value is classified as a RegExp object.

Use the instanceofoperator to check if the provided value is a RegExp object.

const isRegExp = val => val instanceof RegExp;
-
isRegExp(/./g); // true
-

isSet

Checks if value is classified as a Set object.

Use the instanceofoperator to check if the provided value is a Set object.

const isSet = val => val instanceof Set;
-
isSet(new Set()); // true
 

isString

Checks if the given argument is a string.

Use typeof to check if a value is classified as a string primitive.

const isString = val => typeof val === 'string';
 
isString('10'); // true
 

isSymbol

Checks if the given argument is a symbol.

Use typeof to check if a value is classified as a symbol primitive.

const isSymbol = val => typeof val === 'symbol';
 
isSymbol(Symbol('x')); // true
-

isTypedArray

Checks if value is classified as a TypedArray object.

Use the instanceofoperator to check if the provided value is a TypedArray object.

const isTypedArray = val => val instanceof TypedArray;
-
isTypedArray(new TypedArray()); // true
 

isUndefined

Returns true if the specified value is undefined, false otherwise.

Use the strict equality operator to check if the value and of val are equal to undefined.

const isUndefined = val => val === undefined;
 
isUndefined(undefined); // true
 

isValidJSON

Checks if the provided argument is a valid JSON.

Use JSON.parse() and a try... catch block to check if the provided argument is a valid JSON.

const isValidJSON = obj => {
@@ -1283,10 +1285,6 @@ Foo.prototypeShow examples
isValidJSON('{"name":"Adam","age":20}'); // true
 isValidJSON('{"name":"Adam",age:"20"}'); // false
 isValidJSON(null); // true
-

isWeakMap

Checks if value is classified as a WeakMap object.

Use the instanceofoperator to check if the provided value is a WeakMap object.

const isWeakMap = val => val instanceof WeakMap;
-
isWeakMap(new WeakMap()); // true
-

isWeakSet

Checks if value is classified as a WeakSet object.

Use the instanceofoperator to check if the provided value is a WeakSet object.

const isWeakSet = val => val instanceof WeakSet;
-
isWeakSet(new WeakSet()); // true
 

Utility

cloneRegExp

Clones a regular expression.

Use new RegExp(), RegExp.source and RegExp.flags to clone the given regular expression.

const cloneRegExp = regExp => new RegExp(regExp.source, regExp.flags);
 
const regExp = /lorem ipsum/gi;
 const regExp2 = cloneRegExp(regExp); // /lorem ipsum/gi
diff --git a/snippets/is.md b/snippets/is.md
index de07e6070..bd7bb70b5 100644
--- a/snippets/is.md
+++ b/snippets/is.md
@@ -9,7 +9,7 @@ const is = (type, val) => val instanceof type;
 ```
 
 ```js
-is(Array,[1]); // true
+is(Array, [1]); // true
 is(ArrayBuffer, new ArrayBuffer()); // true
 is(Map, new Map()); // true
 is(RegExp, /./g); // true