npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

safe-chain-object

v1.0.6

Published

safe-chain-object 解决 Javascript 项目中,对值进行数据类型,值判断的复杂性,支持无限层级数据判断,支持常用类型判断。

Downloads

3

Readme

如何优雅解决 Javascript 开发过程中数据类型判断或者空值判断

safe-chain-object 解决 Javascript 项目中,对值进行数据类型,值判断的复杂性,支持无限层级数据判断,支持常用类型判断。

Installation and Usage

  • 安装

    npm i safe-chain-object
  • 引用和使用

    import { createSafe as $_} = from 'safe-chain-object';
    const data:any = {code:200,message:'success',data:[]}
    const _data = $_(data)
    if(_data.data.isArray() && _data.code.isEqual(200)){
      //do something
    }
    import { createSafe as $_} = from 'safe-chain-object';
    const data:any = {code:200,message:'success',data:{}}
    const _data = $_(data) //可无限取值, 解决undefined 引发的崩溃问题
    if(_data.data.message.code.isEqual(200)){
      //
      }

更多用法

类型判断。这里提供一个可选链的判断并提供便捷函数。

// 解决无限层级数据判断
class Item{
    id:string;
    name:string;
    create_time:Date;
    child?:Item
}
class FetchData{
    code:Number;
    message:String;
    data:Array<Item>
}
import { createSafe as $_} = from  'safe-chain-object';

function renderItemView(result:FetchData){
    // 为防止参数空值报错,或者人为传值错误
    const chain = $_(result)
    if(chain.code.isEqual(200) && chain.data.isArray() && chain.data.arrayEvery(Item)){
        // code 为200 并且data 为 Item 数组
    }
}

操作符 简化操作

import { createSafe } = from  'safe-chain-object';
import { isNumber ,shape,isString ,isEqual} = from  'safe-chain-object/operators';

// dome1
const source1 =createSafe({data:100});
source1.data.validator(isNumber,isEqual(100))

// dome2
const source2 = createSafe({id:111,title:"说明",info:{code:200,message:'success'}})
source2.validator(shape({
  id:isNumber,
  info:shape({
    code:isEqual(200)
  })
}))

API说明

工具链

instanceof(prototype: Function): boolean;
isObjectTypeOf(targetType: ObjectEnumType | string): boolean;
isNull(): boolean;
isUndefined(): boolean;
isNumber(): boolean;
isString(): boolean;
isBoolean(): boolean;
isFunction(): boolean;
isSymbol(): boolean;
isDate(): boolean;
isRegExp(): boolean;
isPromise(): boolean;
isArray(): boolean;
isMap(): boolean;
isSet(): boolean;
isEqual(target: any): boolean;
isTruly(): boolean;
// 判断数据是否全部为true
/***
* class Item{}
* source.arrayEvery((item)=>{
*  return isItem(item)
* })
*/
arrayEvery<T>(compare: (item: T) => boolean): boolean;
/***
* class Item{}
* source.arrayEvery(Item)
*/
arrayEvery<T>(compare: Function): boolean;
// 空对象 {}
// 空集合 (Array|Set|Map).(length|size) === 0
isEmpty(): boolean;


validator(...args:Function[]):boolean;

操作符

const isEqual: (target: any, isDepth?: boolean) => ExecFunction;
const isNull: ExecFunction;
const isUndefined: ExecFunction;
const isNumber: ExecFunction;
const isString: ExecFunction;
const isBoolean: ExecFunction;
const isObject: ExecFunction;
const isSymbol: ExecFunction;
const isFunction: ExecFunction;
const isDate: ExecFunction;
const isRegExp: ExecFunction;
const isPromise: ExecFunction;
const isArray: ExecFunction;
const isSet: ExecFunction;
const isMap: ExecFunction;
const shape: (space: Space) => ExecFunction;

详细说明

  • 功能说明

    • 安全的数据链

      import { createSafe as $_} = from  'safe-chain-object';
      
      const target = $_({code:100,data:{}})
      // 数据链可无限
      target.data.Xpro.Ypro.Zprop.isEqual(200)
    • typeof 操作符

      import { createSafe as $_} = from  'safe-chain-object';
      
      const target = $_({code:100,data:{ id:10000,items:[] }})
      
      typeof target.data.id === 'function'
      typeof target.data.item === 'function'
      typeof target.data.item.forEach === 'function'
    • 解构

      import { createSafe as $_} = from  'safe-chain-object';
      
      const target = $_({code:100,data:{ id:10000,items:[1,2,3] }})
      // 对象解构
      const  { id, items } =  target.data;
      // 数组解构
      const [$1,$2] = target.date.items;
    • 函数

      import { createSafe as $_} = from  'safe-chain-object';
      
      const source = {code:100,data:{ id:10000,items:[1,2,3] }}
      const target = $_(source)
      // target.a.b.c.d() 可执行,return NULL;
      // 可行 但不推荐。
      target.data.items.forEach(callback)
      //推荐写法
      if(target.data.items.isArray()){
          source.data.items.forEach
      }
    • instanceof 类型判断

      import { createSafe as $_} = from  'safe-chain-object';
      
      const target = $_({item:[]})
      target.item instanceof Array // success
      target.item instanceof Number // fail
      target.sa.a.sas.as  instanceof Object 始终成立
      
      target.sa.a.sas.as  instanceof Function 始终成立
      ==============
      class Item {}
      const target = $_({item:new Item()})
      target.item instanceof Item // true
      
    • Object.prototype.toString.call 可正常使用

      /**
          
       ** 可自行实现 Symbol.toStringTag
          
       */
      
      const toTypeString = Function.call.bind(Object.prototype.toString);
      
      const target = createSafe({ a: [], b: 1 });
      
      toTypeString(target) === '[object Object]';
      
      toTypeString(target.a) === '[object Array]';
      
      toTypeString(target.b) === '[object Number]';
      
      toTypeString(target.sasb.sasa.asa.acdd) === '[object Undefined]';

Dome 测试

  • 浏览器打开 /test/index.html