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

js-data-model

v1.2.6

Published

一个让表单转换稍微简单那么一点点的库~

Downloads

5

Readme

这是啥

这是一个让表单转换稍微简单那么一点点的库~

有啥用

通过配置 model 的方式转换出一个可预期的 js 对象

啥时要

当你觉得数据转换写一堆 if else || switch case 不好看, 不好写的时候

咋子用

  • script标签引入
const {DataModel, parserTypes} = JsDataModel
const model = new DataModel(/** modelOptions */)

model.parse({}) // modelOptions -> an Object formated with model
  • esModule导入
import {DataModel, parserTypes} from 'js-data-model'

const model = new DataModel(
  aKey: {
    type: parserTypes.Number,
    default: 1
  }
) 

model.parse({}) // modelOptions -> an Object formated with model

更多例子可参考test/*

构造参数

model

interface Model {
  [key: any]: Options
}

interface Options {
  type?: any
  subModel?: OptionsMap
  default?: any
  parser?: CustomParser
  fromKey?: string
}

参数 | 说明 --- | --- type | parser 的类型, 可使用 parserTypes.* subModel | model, 用来表示objectarray下一层级的类型 default | 内置 parser 返回 nullundefined 时, 使用这个值返回, 如果返回一个对象需要是一个工厂函数 parser | 自定义 parser fromKey | 初始值来源的 key, 默认是 model 的 key

实例方法

parser

  type Parser = (val: any, model: Model, helperData?: any) => any

静态方法

addParserTypes

addParserTypes(types: Record<string, any>): void

addParser

addParser(type: any, parser: Parser): void

use

use(plugin: DataModelPlugin, options?: any): void

内置 parser 转换规则

val: 初始值

Number

初始值 | 转换结果 --- | --- null | undefined undefined | undefined string | val number | Number(val) object | NaN array | NaN

String

初始值 | 转换结果 --- | --- null | undefined undefined | undefined string | String(val) number | val object | JSON.stringify(val) array | JSON.stringify(val)

Boolean

初始值 | 转换结果 --- | --- null | false undefined | false '' | false string | true 0 | false number | true object | true array | true 本质是!val

Object

初始值 | 转换结果 --- | --- null | {} undefined | {} string' | {} number | {} object | 浅克隆(val) 带有 subModel 的 object | {} as typeof subModel array | {}

Array

初始值 | 转换结果 --- | --- null | [] undefined | [] string | [] number | [] object | [] array | 浅克隆(val) 带有 subModel 的 array | (typeof subModel)[]

TODO

  • parserTypes.Array subModel 为 parserTypes 的值之一
  • type 字段支持传入 DataModel 的实例