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

dom-data-bridge

v2.0.1

Published

js读取写在dom内的数据

Downloads

14

Readme

dom-data-bridge

核心功能:解析string格式json数据

安装

// npm 
npm install dom-data-bridge  // 装载
npm update dom-data-bridge   // 更新

// yarn
yarn add dom-data-bridge     // 装载
yarn upgrade dom-data-bridge // 更新

// Browserify(https://github.com/songyijian/dom-data-bridge)
<script src="../dist/index.js"></script> 

引入

import {
  DataDridge,
  parseMuster,
  superParse,
  domDataDridge,
} from 'dom-data-bridge'

domDataDridge

// domDataDridge会过滤: data-dridge 属性的dom ; data-dridge="key" ,把dom内容作为 val

<textarea data-dridge=array id="array" rows="2">
[ '1',2]
</textarea>


let demo = domDataDridge()
    // demo === retrun mew DataDridge(creatfilter)
    // demo.push() === new DataDridge.push()
    // demo.get() === new DataDridge.get()

    // creatfilter push 前的回调
    function creatfilter({key,val}) {
      if (key === ''){
        key = 'object'
        val = 'data-dridge, key为空被creatfilter过滤处理'
      }
      return {key, val}
    }

DataDridge

整理解析不标准数据,按照规则读取。 更多细节见demo/index

var globalConfig = { // 全局配置
  risk:  false, // <superParse> 接受风险解析非标准json
  filter: a => a  // 过滤函数
}

const demo = new DataDridge(globalConfig)
      demo.dataMap = {}   // 原始数据源
      demo.config = globalConfig // 全局排除规则


/**
 * @Description: 插入数据
 * @param {object|'{}'|key} wkey 【json|‘{}’】会被解析成对象合并到dataMap,【string】生成{[wkey]:val}
 * @param {*} val 当wkey=‘{}’时val必填
 * @param {*} risk wkey=‘{}’ 是否利用superParse解析
 */
demo.push( wkey, val = '', risk = globalConfig.risk )
  // demo.push( '{"a":1}') // yes
  // demo.push( 'a', '1',) //yes
  // demo.push( '{a:1}', '', true) //yes
  // demo.push( {a:1}, '',fasle) //error
  // demo.push( '{a:1}', '') // error
  // demo.push( '[]', '', true) //error

/**
 * @Description: 根据(解析|验证规则)获取数据
 * @param {object} schema {filter:function,risk:boolean,type:<parseMuster>,default} 解析|验证规则
 * @param {boolean} risk parseMuster规则
 * @return {object} schema 验证通过的{}
 */
get(schema, risk = globalConfig.risk)  

  schema = {
    key:{
      filter:(a)=>a,
      risk:false,
      type: "Object|Array|String|Boolean|Number|RegExp", //  parseMuster| RegExp正则验证
      default:2 // 默认值
    }
  }

  // 数据处理流程
  // filter > type( Object|Array ? risk) > default

parseMuster

superParse > parseMuster ,DataDridge 内的解析(验证)规则

let {Object, Array, String, Boolean, Number} = parseMuster;

/**
 * @Description: 解析验证数据类型
 * @param {Object, Array, String, Boolean, Number} a 要解析的'{字符串}'对象, 不可‘[]’
 * @param {boolean} risk 利用eval可以解析一些不规范的{json}
 * @throw {Error} 解析+验证 失败
 */
parseMuster[key](a,risk) // Object&Array == <superParse>

  // Object {}
  parseMuster.Object('{a:1}') // Error
  parseMuster.Object('{a:1}',true) // {a:1}
  parseMuster.Object('{"a":1}') // {a:1}
  parseMuster.Object({a:1},true)  // {a:1}

  // Array []
  parseMuster.Array('{a:1}',true) // Error
  parseMuster.Array('[{a:1},2]') // Error
  parseMuster.Array('[{a:1},2]',true) // [{…}, 2]

  // Number 对NaN做了处理
  parseMuster.Number == Number() 
  parseMuster.Number(NaN) //Error

  parseMuster.String === String()
  parseMuster.Boolean === Boolean()

superParse

superParse 字符串json解析,可针对非标准的数据字符串开启风险解析,

/**
 * @Description: 支持高风险的json字符串解析
 * @param {string} strs 要解析的'{字符串}'对象, 不可‘[]’
 * @param {boolean} risk 利用eval可以解析一些不规范的{json}
 * @return {object} 
 */
superParse(string,risk)
  // string : '{}' | '[]'
  // risk :true = 可以解析一些不规范的{json} 但是存在风险,谨慎使用

  superParse('{a:1}') // Error
  superParse('{"a":1}') // {a:1}
  superParse('{a:1}',true) // {a:1}
  superParse('[{a:1},2]') // Error
  superParse('[{a:1},2]',true) // [{…}, 2]