飞跃高山与大洋的鱼飞跃高山与大洋的鱼
首页
先看
计算机
  • 数学
  • Linux
  • Arch
  • Manjaro
  • Ubuntu
  • CentOS
  • Kubernetes
  • Web
  • JavaScript
  • TypeScript
  • CSS
  • Canvas
  • Vue
  • Vite
  • NuxtJS
  • Webpack
  • Flutter
  • D3
  • Jest
  • WeApp
  • Utils
  • Nodejs
  • Nestjs
  • Golang
  • Nginx
  • Traefik
  • MySQL
  • MongoDB
  • Redis
  • Docker
算法
  • 像素风
  • Git
  • Github
  • VSCode
  • Chrome
  • Google
  • Bookmark scripts
  • 导航 🎉
  • VuePress 侧边栏插件
  • VuePress 官网
🚇 开往
首页
先看
计算机
  • 数学
  • Linux
  • Arch
  • Manjaro
  • Ubuntu
  • CentOS
  • Kubernetes
  • Web
  • JavaScript
  • TypeScript
  • CSS
  • Canvas
  • Vue
  • Vite
  • NuxtJS
  • Webpack
  • Flutter
  • D3
  • Jest
  • WeApp
  • Utils
  • Nodejs
  • Nestjs
  • Golang
  • Nginx
  • Traefik
  • MySQL
  • MongoDB
  • Redis
  • Docker
算法
  • 像素风
  • Git
  • Github
  • VSCode
  • Chrome
  • Google
  • Bookmark scripts
  • 导航 🎉
  • VuePress 侧边栏插件
  • VuePress 官网
🚇 开往
  • UTILS

    • JavaScript 工具集
    • 数组
    • 浏览器
    • 日期
    • 函数
    • 数学
    • Node
    • 对象
    • 字符串
    • 类型判断处理
      • 判断
        • 1. is(指定类型)
        • 2. isArrayLike(类数组)
        • 3. typeof
        • 4. isEmpty
        • 5. isNil
        • 6. isNull/isUndefined
        • 7. isNumber
        • 8. isObjectLike
        • 9. isPlainObject(普通对象)
        • 10. isValidJSON(JSON)
      • 处理
        • 1. getType(获取类型)
        • 2. castArray(转为数组)
        • 3. coalesceFactory(指定返回类型)

类型判断处理

判断

1. is(指定类型)

FROM

30 seconds of code (Type)

FUNCTION:

const is = (type, val) => ![, null].includes(val) && val.constructor === type;

EXAMPLES:

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, ''); // true
is(String, new String('')); // true
is(Number, 1); // true
is(Number, new Number(1)); // true
is(Boolean, true); // true
is(Boolean, new Boolean(true)); // true

2. isArrayLike(类数组)

FROM

30 seconds of code (Type)

FUNCTION:

const isArrayLike = obj => obj != null && typeof obj[Symbol.iterator] === 'function';

EXAMPLES:

isArrayLike([1, 2, 3]); // true
isArrayLike(document.querySelectorAll('.className')); // true
isArrayLike('abc'); // true
isArrayLike(null); // false

3. typeof

FROM

30 seconds of code (Type)

FUNCTION:

const isBoolean = val => typeof val === 'boolean';
const isFunction = val => typeof val === 'function';
const isString = val => typeof val === 'string';
const isSymbol = val => typeof val === 'symbol';
const isUndefined = val => typeof val === 'undefined';

EXAMPLES:

isBoolean(null); // false
isBoolean(false); // true
isFunction('x'); // false
isFunction(x => x); // true
isString('10'); // true
isSymbol(Symbol('x')); // true
isUndefined(); // true

4. isEmpty

FROM

30 seconds of code (Type)

FUNCTION:

const isEmpty = val => val == null || !(Object.keys(val) || val).length;

EXAMPLES:

isEmpty([]); // true
isEmpty({}); // true
isEmpty(''); // true
isEmpty([1, 2]); // false
isEmpty({ a: 1, b: 2 }); // false
isEmpty('text'); // false
isEmpty(123); // true - type is not considered a collection
isEmpty(true); // true - type is not considered a collection

5. isNil

FROM

30 seconds of code (Type)

FUNCTION:

const isNil = val => val === undefined || val === null;

EXAMPLES:

isNil(null); // true
isNil(undefined); // true
isNil(''); // false

6. isNull/isUndefined

FROM

30 seconds of code (Type)

FUNCTION:

const isNull = val => val === null;
const isUndefined = val => val === undefined;

EXAMPLES:

isNull(null); // true
isUndefined(undefined); // true

7. isNumber

FROM

30 seconds of code (Type)

FUNCTION:

const isNumber = val => typeof val === 'number' && val === val;

EXAMPLES:

isNumber(1); // true
isNumber('1'); // false
isNumber(NaN); // false

8. isObjectLike

FROM

30 seconds of code (Type)

FUNCTION:

const isObjectLike = val => val !== null && typeof val === 'object';

EXAMPLES:

isObjectLike({}); // true
isObjectLike([1, 2, 3]); // true
isObjectLike(x => x); // false
isObjectLike(null); // false

9. isPlainObject(普通对象)

FROM

30 seconds of code (Type)

FUNCTION:

const isPlainObject = val => !!val && typeof val === 'object' && val.constructor === Object;

EXAMPLES:

isPlainObject({ a: 1 }); // true
isPlainObject(new Map()); // false

10. isValidJSON(JSON)

FROM

30 seconds of code (Type)

DETAIL:

判断是否为有效 JSON。利用 try...catch 来处理错误。

FUNCTION:

const isValidJSON = str => {
  try {
    JSON.parse(str);
    return true;
  } catch (e) {
    return false;
  }
};

EXAMPLES:

isValidJSON('{"name":"Adam","age":20}'); // true
isValidJSON('{"name":"Adam",age:"20"}'); // false
isValidJSON(null); // true

处理

1. getType(获取类型)

FROM

30 seconds of code (Type)

FUNCTION:

const getType = v => (v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name);

EXAMPLES:

getType(new Set([1, 2, 3])); // 'Set'

2. castArray(转为数组)

FROM

30 seconds of code (Type)

FUNCTION:

const castArray = val => (Array.isArray(val) ? val : [val]);

EXAMPLES:

castArray('foo'); // ['foo']
castArray([1]); // [1]

3. coalesceFactory(指定返回类型)

FROM

30 seconds of code (Type)

DETAIL:

自定判断函数,includes 方法果然好用。

FUNCTION:

const coalesceFactory = valid => (...args) => args.find(valid);

EXAMPLES:

const customCoalesce = coalesceFactory(_ => ![null, undefined, '', NaN].includes(_));
customCoalesce(undefined, null, NaN, '', 'Waldo'); // "Waldo"
编辑文档!
上次更新:
贡献者: shanyuhai123
Prev
字符串