Giter VIP home page Giter VIP logo

jsobject's Introduction

JS对象字符串化及解析,支持函数和正则对象

NPM Version NPM Downloads

安装

$ npm i -S js-object-stringify

特性说明

支持 AMD、CMD、CommonJs 方式引入

使用方法和 JSON.stringify() 一致

支持的特殊类型包括

  • Function
  • RegExp
  • Date
  • undefined

其余类型会默认调用 JSON.stringify() 方法完成字符串化

方法定义

/**
 * JS对象字符串化及解析,支持函数、正则对象和日期对象
 * @namespace JSObject
 */
var JSObject = {
    /**
     * 将js对象转为字符串,支持函数和正则对象
     * @param {object} obj js对象,支持 object、array、string、number、boolean、RegExp、Date、undefined、null
     * @param {function} [replacer] 替换函数
     * @param {number} [space] 空格缩进
     * @returns {string} 格式化的js对象字符串
     */
    stringify(value[, replacer [, space]]),

    /**
     * 将js字符串转换为对象
     * @param {string} str 格式化的js对象字符串
     * @returns {object} js对象
     */
    parse(str)
};

调用示例

const JSObject = require('js-object-stringify');

const obj = {
    reg: /./i,
    date: new Date('1990-11-10T03:42:00.000Z'),
    fun: (...args) => {
        console.log(args);
    }
};

JSObject.stringify(obj, null, 4);
// "{\n    \"reg\": /./i,\n    \"date\": new Date(\"1990-11-10T03:42:00.000Z\"),\n    \"fun\": (...args) => {\n        console.log(args);\n    }\n}"

jsobject's People

Contributors

xingqiao avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.