1.0.2 • Published 2 years ago

nv-json-stype v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

nv-json-stype

  • nv-json-stype
  • get the structure-info of json ignoring the data-type
  • get json from structure-desc ignoring the data-type

install

  • npm install nv-json-stype

usage

  const {
      creat_tmpl_from_desc,
      creat_desc_from_json,
      json_is_type_of_tmpl,
  } = require("nv-json-stype")

  ////
  Desc  is a json which only permit  null AND int leaf elements
   


  PlaceHolder : null | undefined              default is null
  Repeater    : Int
  ArrayDesc   : Array<Repeater|ArrayDesc|DictDesc>
  DictDesc    : Object<[k:string]:ArrayDesc|DictDesc|PlaceHolder>
  Desc        : ArrayDesc | DictDesc  

example

    var desc = {
        a: [
            3,  /*means 3 leaf element slots here*/
            {x:undefined},
            2,  /*means 2 leaf element slots here*/
            [  
                4  /*means 4 leaf element slots here*/
            ],
            1   /*means 1 leaf element slots here*/
        ],
        b:undefined,
        c:{y:[5 /*means 5 leaf element slots  here*/,{}]},
    }



    var tmpl = creat_tmpl_from_desc(desc)

    /*
    > tmpl
    {
      a: [
        null,
        null,
        null,
        { x: null },
        null,
        null,
        [ null, null, null, null ],
        null
      ],
      b: null,
      c: { y: [ null, null, null, null, null, {} ] }
    }
    >

    */
    var J = {
      a: [
        'xx',
        1111,
        2222,
        { x: null },
        'yyy',
        null,
        [ false, null, true, null ],
        null
      ],
      b: null,
      c: { y: [ null, null, 888, null, null, {} ] }
    }


    > creat_desc_from_json(J)
    { a: [ 3, { x: null }, 2, [ 4 ], 1 ], b: null, c: { y: [ 5, {} ] } }
    >

    > var desc = {
    ...   a: [ 3, { x: undefined }, 2, [ 4 ], 1 ],
    ...   b: undefined,
    ...   c: { y: [ 5, {} ] }
    ... }
    >
    > var tmpl = creat_tmpl_from_desc(desc);
    >
    >
    >
    > var J = {
    ...   a: [
    ...     'xx',
    ...     1111,
    ...     2222,
    ...     { x: null },
    ...     'yyy',
    ...     null,
    ...     [ false, null, true, null ],
    ...     null
    ...   ],
    ...   b: null,
    ...   c: { y: [ null, null, 888, null, null, {} ] }
    ... }
    undefined
    >
    > json_is_type_of_tmpl(J,tmpl)
    [ true, null, null ]
    >

APIS

 only_normal_dict   means:
      TREAT 
         set | map | wkset   | wkmap  | wkref
         function  | class   | lambda | func_expr | cls_expr |
         generator | promise | proxy  | 
         typed_array | buf_like | data_view |
         regexp    | date 
      AS leafs
 default is true

 only_normal_dict set to false will increase the performance,but will treat any object as dict
 if J is pure JSON, set only_normal_dict to false

  • creat_tmpl_from_desc(desc,placeholder=null,only_normal_dict=true)
  • creat_desc_from_json(J,placeholder=null,only_normal_dict=true)
  • json_is_type_of_tmpl(J,tmpl,only_normal_dict=true):boolean,pl,reason

LICENSE

  • ISC