3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScriptの型判定モジュールを書く

Last updated at Posted at 2018-03-17

TL; TD;

JavaScriptの型は勝手に変わったり判定が難しかったりすのでisUtilとしてまとめる。
大体は標準関数に渡すだけ何を返すかは標準関数任せ。
例外は投げられないようにして返り値はすべてbooleanにした(はず)。

実装

isUtil.js
const isUtil={
    isUndefined: (target)=>{ return target===void 0; },
    isNull: (target)=>{ return target==null; },                                                

//    isObject:   (target)=>{ return typeof target==='object';   }, nullに対応
    isObject:   (target)=>{ return target === null ? false : typeof target==='object'; },
    isBoolean:  (target)=>{ return typeof target==='boolean';  },
    isString:   (target)=>{ return typeof target==='string';  },
    isNumber:   (target)=>{ return typeof target==='number';  },                                                             
    isFunction: (target)=>{ return typeof target==='function'; },                                       

    isTrue:  (target)=>{ return target===true; }, // return 'true'=> false                                                                     
    isFalse: (target)=>{ return target===false; },
    isArray: (target)=>{ return Array.isArray(targert); },

    isEmpty: (target)=>{
        if( target==null ) return true;
        else if( target.length!=null && target.length===0 ) return true; // return "", [] => true;                                             
        else if( typeof target==='object' && Object.keys(target).length===0 ) return true; // {} => true;                                      
        else return false;
    },
    isInstance: (target)=>{
        try{
            if( {} instanceof target ){}
            return true;
        }
        catch(e){ return false; }
    }
}

module.exports=isUtil;

判定は厳密なので"true"はfalseを返す。isNumber("1")false
例外はisNull=(target)=>{ return target==null; }

検証用コード

isUtil/test/isFunc.js
const func=function(){}
const arrFunc=()=>{};
const asyncFunc= async ()=>{};
const arrAsyncFunc= async function(){};

console.log(typeof arrFunc);
console.log(typeof arrAsyncFunc);
console.log(typeof func);
console.log(typeof asyncFunc);

console.log(typeof null);

一応テストもあるよ

走らせる前に答えを考えてみてね。

isUtil/test/isInstance.js
'use strict'
const isUtil=require('../../isUtil');

const aa={};
const a = class{};
const f = function(){};
const func=()=>{};

console.log(isUtil.isInstance(Date));
console.log(isUtil.isInstance(RegExp));

console.log(isUtil.isInstance(new Date));
console.log(isUtil.isInstance(new RegExp));

console.log(isUtil.isInstance(''));
console.log(isUtil.isInstance(String));
console.log(isUtil.isInstance(String('')));

console.log(isUtil.isInstance(f));
console.log(isUtil.isInstance(func));

console.log(isUtil.isInstance(a));
console.log(isUtil.isInstance(aa));

最後に

どこかに上げるほどのものでもないと思うのでコピペして使ってください。
どのブラウザでも動くと思いますが実行環境はNodeを想定しています。
例外が投げられれた、思った返り値にならないなどの報告はコメントでおねがいします

3
2
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?