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

parse-string

v1.2.8

Published

Parse the string into a Map.

Downloads

26

Readme

parse-string

Parse the string into a Map.

NPM Version NPM Downloads Build Status Gratipay

Installation

$ npm install parse-string
#
$ yarn add parse-string

Features

  • parse string into Map
  • format data
  • filter and verify data.
  • verify signature.

API

toValue (type: 'string' | 'number' | 'date' | 'map' = 'string'): (value: any) => any

Convert specified type value.

  • type - target type
  • value - value of need convert

formatData (formats?: ParseData.format | ParseData.format[], customize?: Record<string, Function>): (value: any) => any

Format Data.

  • formats - formatting options
  • customize - map of custom function
  • value - value of need format

parseData (options: ParseData.options, customize?: Record<string, Function>): (data: string) => Record<string, any>

Parse the string to the specified result.

  • options - parsing options
  • customize - map of custom function
  • data - data of need parse

parseBody (options: ParseData.parse[], customize?: Record<string, Function>): (msgbody: Record<string, any>) => Record<string, any>

Parse the msbody to the specified result.

  • options - parsing options
  • customize - map of custom function
  • msbody - msbody of need parse

filterData (options: FilterData.options[], customize?: Record<string, Function>): (data: Record<string, any>) => Record<string, any>

Filter and verify data.

  • options - filter options
  • customize - map of custom function
  • data - data of need filter

validSign (options: string, sign: string = 'sign'): (data: Record<string, any>) => boolean

Verify signature.

  • options - style of signature
  • sign - feild of signature
  • data - data of submit

Usages

Example: Parse string

import { parseData } from 'parse-string'

const customize = {
  add: (a, b) => a + b
}

const options = {
  separator: /\;/,
  collection: [
    {
      key: 'date',
      type: 'string',
      format: [
        {
          type: 'string',
          regexp: /^(\d{4})(\d{2})(\d{2})$/,
          substr: '$1-$2-$3'
        },
        {
          type: 'date'
        }
      ]
    },
    {
      key: 'amount',
      type: 'number'
    },
    {
      key: 'user',
      type: 'map'
    },
    {
      key: 'username',
      result: {
        defaultValue: '$__user'
      },
      format: {
        type: 'map',
        maps: 'username'
      }
    },
    {
      key: 'group',
      result: {
        defaultValue: '$__user'
      },
      format: {
        type: 'map',
        maps: 'group.name'
      }
    },
    {
      key: 'level',
      result: {
        defaultValue: '$__user'
      },
      format: {
        type: 'map',
        maps: 'group.level'
      }
    },
    {
      key: 'money1',
      result: {
        defaultValue: '$__amount',
        formula: {
          exec: (a, b) => a + b,
          opts: [ '$__amount', '$__level' ]
        }
      }
    },
    {
      key: 'money2',
      result: {
        defaultValue: '$__amount',
        formula: {
          exec: 'add',
          opts: [ '$__amount', '$__level' ]
        }
      }
    }
  ],
  omits: [ 'user' ]
}

const data = '20201027;39554;{username:\'thondery\',group:{name:\'管理员\',level:9999}}'

parseData(options, customize)(data)
// { 
//   date: 2020-10-27T00:00:00.000Z,
//   amount: 39554,
//   username: 'thondery',
//   group: '管理员',
//   level: 9999,
//   money1: 49553,
//   money2: 49553 
// }

Example: Filter and verify data

import { filterData, validSign } from 'parse-string'

const customize = {
  isPassword: value => /^(?=.*[A-Za-z])[A-Za-z0-9$@$!%*#?&]/.test(value)
}

const options = [
  {
    key: 'username',
    type: 'string',
    rules: [
      { required: true, message: '用户名不能为空' },
      { min: 4, max: 12, message: '用户名长度不能小于4或大于12(字符)' },
      { pattern: '^[a-zA-Z]{1}[a-zA-Z0-9\_\-]', message: '用户名格式错误' }
    ]
  },
  {
    key: 'password',
    type: 'string',
    rules: [
      { required: true, message: '密码不能为空' },
      { min: 6, max: 15, message: '密码长度不能小于6或大于15(字符)' },
      { validator: 'isPassword', message: '密码格式错误' }
    ]
  },
  {
    key: 'items',
    type: 'string[]',
    defaultValue: []
  },
  {
    key: 'sign',
    type: 'string',
    md5: '${password}${username}'
  }
]

const data = { username: 'thondery', password: 'a123456', items: '1001,1002,1003' }

try {
  let result = filterData(options, customize)(data)
  // {
  //   username: 'thondery', 
  //   password: 'a123456', 
  //   items: ['1001', '1002', '1003'],
  //   sign: '61a0375131b33b72b56e4e244d0b2f29'
  // }
} catch (error) {
  console.error(error.message)
}

validSign('${password}${username}', 'sign')({ username: 'thondery', password: 'a123456', sign: '61a0375131b33b72b56e4e244d0b2f29' })
// true or false

License

this repo is released under the MIT License.