Code Monkey home page Code Monkey logo

jsonuri's Introduction

JSON URI


Use URI style methods to operate data. All operations friendly support Vue-like frameworks.

Build Status codecov npm dependencies Status devDependencies Status

Use

$ npm install jsonuri --save
import * as jsonuri from 'jsonuri'
// or
import { get, set, ... } from 'jsonuri' // recommended practice, friendly to tree-shaking

Example Data:

{
  "menu": {
    "id": 123,
    "list": [0, 1, 2, 3, 4],
    "popup": {
      "menuitem": [{
          "value": "New",
          "onclick": "CreateNewDoc()"
        },
        {
          "value": "Open",
          "onclick": "OpenDoc()"
        },
        {
          "value": "Close",
          "onclick": "CloseDoc()"
        }
      ]
    }
  }
}

Methods:

get (data, path)

Get the value of the specified data for the path.

Example:

jsonuri.get(data, 'menu/id')
// return 123

jsonuri.get(data, 'menu/popup/menuitem/0/value')
// return 'New'

jsonuri.get(data, 'menu/popup/menuitem/0/value/..')
// {value: "New", onclick: "CreateNewDoc()"}

see more

set (data, path, value)

Set the value of the specified data for the path.

Example:

jsonuri.set(data, 'menu/id/', 789)
jsonuri.get(data, 'menu/id')
//789

see more

rm (data, path)

Remove the value of the specified data for the path.

Example:

jsonuri.rm(data, 'menu/id')
jsonuri.get(data, 'menu/id') // undefined

see more

mv (data, pathA, pathB, sequence)

Data A moved to target B before or after.

Example:

jsonuri.mv(data, 'menu/list/0', 'menu/list/3')
jsonuri.get(data, 'menu/list') // [1, 2, 3, 0, 4]
[see more](test/spec/mv_spec.js)


jsonuri.set(data, 'menu/list/',[0,1,2,3,4])
jsonuri.mv(data, 'menu/list/0', 'menu/list/3', 'before')
jsonuri.get(data, 'menu/list') // [1, 2, 0, 3, 4]

see more

swap (data, pathA, pathB)

Data swap in an array.

Example:

jsonuri.swap(data, 'menu/list/0', 'menu/list/4')
jsonuri.get(data, 'menu/list') // [4, 1, 2, 3, 0]

jsonuri.swap(data, 'menu/list/0', 'menu/list/4')
jsonuri.get(data, 'menu/list') // [4, 1, 2, 3, 0]

see more

insert (data, pathA, value, direction)

Insert data into an array that is described in the path.

Example:

jsonuri.insert(data, 'menu/list/0', 9999, 'before') // [9999, 0, 1, 2, 3, 4]

see more

up(data, path, gap)

see more

down(data, path, gap)

see more

walk(data, descentionFn, ascentionFn)

Traverse each data of each node and value.

Example:

jsonuri.walk({a:{a1:'x'}}, (value, key, parent, { path }) => {
  console.log(value, key, parent, path)
})

// { a1: 'x' } 'a' { a: { a1: 'x' } } 'a'
// x a1 { a1: 'x' } 'a/a1'

see more

normalizePath(path1, path2, ...)

Example:

jsonuri.normalizePath('a', 'b') // a/b

jsonuri.normalizePath(['a', 'b', '../'], 'c') // a/c

see more

isCircular(obj)

Example:

jsonuri.isCircular({}) // return false
jsonuri.isCircular(window) // return true

var a = {}
jsonuri.set(a, '/b/c/d/e/f/g', a)
jsonuri.isCircular(a) // return true

see more

jsonuri's People

Contributors

haozi avatar height avatar jaryway avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

jsonuri's Issues

对深层数据执行 mv 操作后,from 对象没有被移除

// jsonuri.set(vm.data, "batters/batter/length", 1);
var dta;
jsonuri.set(
  [
    {
      name: "a",
      // 0/children
      children: [
        // 0/children/0
        { name: "a-0" },
        // 0/children/1
        {
          name: "a-1",
          // 0/children/1/children
          children: [
            // 0/children/1/children/0
            { name: "a-1-0" },
            // 0/children/1/children/1
            { name: "a-1-1" },
            { name: "a-1-2" },
            { name: "a-1-3" },
          ],
        },
        // 0/children/2
        {
          name: "a-2",
          // 0/children/2/children
          children: [
            // 0/children/2/children/0
            { name: "a-2-0" },
            // 0/children/2/children/1
            {
              name: "a-2-1",
              // 0/children/2/children/1/children
              children: [
                { name: "a-2-1-0" },
                // 0/children/2/children/1/children/1
                { name: "a-2-1-1" },
                { name: "a-2-1-2" },
                { name: "a-2-1-3" },
              ],
            },
            { name: "a-2-2" },
            { name: "a-2-3" },
          ],
        },
      ],
    },
  ],
  "0/name",
  "aaaaa"
);
var data = [
  {
    name: "a",
    // 0/children
    children: [
      // 0/children/0
      { name: "a-0" },
      // 0/children/1
      {
        name: "a-1",
        // 0/children/1/children
        children: [
          // 0/children/1/children/0
          { name: "a-1-0" },
          // 0/children/1/children/1
          { name: "a-1-1" },
          // 0/children/1/children/2
          { name: "a-1-2" },
          // 0/children/1/children/3
          { name: "a-1-3" },
        ],
      },
      // 0/children/2
      {
        name: "a-2",
        // 0/children/2/children
        children: [
          // 0/children/2/children/0
          { name: "a-2-0" },
          // 0/children/2/children/1
          {
            name: "a-2-1",
            // 0/children/2/children/1/children
            children: [
              { name: "a-2-1-0" },
              // 0/children/2/children/1/children/1
              { name: "a-2-1-1" },
              { name: "a-2-1-2" },
              { name: "a-2-1-3" },
            ],
          },
          { name: "a-2-2" },
          { name: "a-2-3" },
        ],
      },
    ],
  },
];
// 把 a-2-1-1 移到 0/children/0 前面;

// 期望:a-2-1-1 移到 0/children/0 前面,并且从 0/children/2/children/1/children/1 移除
// 实际:a-2-1-1 移到 0/children/0 前面,但未从 0/children/2/children/1/children/1 移除

jsonuri.mv(data, "0/children/2/children/1/children/1", "0/children/0", "before");
console.log(data);

react 中使用,不能使页面更新,该如何使用呢?

const [test, setTest] = useState<any>([
    { label: 'a', children: [{ label: 'a-0' }, { label: 'a-1', children: [{ label: 'a-0' }, { label: 'a-1' }] }] },
    { label: 'b', children: [{ label: 'b-0' }, { label: 'b-1' }] },
  ]);

  console.log('render', test);

  return (
    <>
       <div className='example-app'>
        <Button
          onClick={() => {
            const next = test;
            mv(next, '1/children/1', '0/children/1/children/1', 'before');
            console.log('test', test, next);
            setTest(next);
          }}
        >
          Click
        </Button>
      </div>
    </>

setTest 之后并没有 log render

Trying to get in touch regarding a security issue

Hey there!

I'd like to report a security issue but cannot find contact instructions on your repository.

If not a hassle, might you kindly add a SECURITY.md file with an email, or another contact method? GitHub recommends this best practice to ensure security issues are responsibly disclosed, and it would serve as a simple instruction for security researchers in the future.

Thank you for your consideration, and I look forward to hearing from you!

(cc @huntr-helper)

walk需要支持break和continue, 急需支持

比如: var data = { a: { aa: { aaa: ‘aaa’ }, isopen: true }, b: { bb: 'bb'} }
walk(data, function(obj, key, raw, path){
if(obj.isopen==true){
return true; //标示当前对象不再遍历, 类似 continue;
return false: //总个跳出遍历,类似break
}else{
set(data, path.path+'data-path/', path.path);
}
})
// 输出: { a: { aa: { aaa: ‘aaa’ }, isopen: true }, b: { bb: 'bb’, data-path: ‘….’} , data-path: ‘…’}

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.