• Stars
    star
    132
  • Rank 274,205 (Top 6 %)
  • Language
    TypeScript
  • Created about 5 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Path Matcher/Getter/Setter for Object/Array

Depath

Path utils for match/getter/setter

Usage

import { Path } from "depath"

const path = new Path("a.b.*")

path.match(["a","b","c"]) // true


Install

npm install --save depath

API

  • Constructor

    • new Path(pattern : string | number | Path | Array<string | number>)
  • Methods/Properties

    • concat(...args: Array<string | number>)

    • slice(start?: number, end?: number)

    • push(item: string | number)

    • pop()

    • splice(start: number,deleteCount?: number,...items: Array<string | number>)

    • forEach(callback: (key: string | number)

    • map(callback: (key: string | number)

    • reduce<T>(callback: (buf: T, item: string | number, index: number) => T, initial: T) : T

    • parent()

    • includes(pattern: Pattern)

    • transform<T>(regexp: string | RegExp,callback: (...args: string[]) => T) : T

    • match(pattern: Pattern)

    • getIn(source?: any)

    • setIn(source?: any, value?: any)

    • deleteIn(source?: any)

    • existIn(source?: any,start?:number | Path)

  • Static Methods

    • parse(pattern: Pattern)

    • getIn(source: any, pattern: Pattern)

    • setIn(source: any, pattern: Pattern, value: any)

    • deleteIn(source: any, pattern: Pattern)

    • existIn(source: any,pattern: Pattern,start?:number | Path)

    • transform<T>(pattern: Pattern,regexp: string | RegExp,callback: (...args: string[]) => T):T

Getter/Setter Destructor Syntax

  • Object Pattern

    Path.setIn({},'a.b.c.{aaa,bbb}',{aaa:123,bbb:321})
    ==>
    {a:{b:{c:{aaa:123,bbb:321}}}}
    Path.getIn({a:{b:{c:{aaa:123,bbb:321}}}},'a.b.c.{aaa,bbb}')
    ==>
    {aaa:123,bbb:321}
    
    Path.setIn({a:{b:{c:{kkk:'ddd'}}}},'a.b.c.{aaa,bbb}',{aaa:123,bbb:321})
    ==>
    {a:{b:{c:{aaa:123,bbb:321,kkk:'ddd'}}}}
    Path.getIn({a:{b:{c:{aaa:123,bbb:321,kkk:'ddd'}}}},'a.b.c.{aaa,bbb}')
    ==>
    {aaa:123,bbb:321}
    
    Path.setIn({a:{b:{c:{kkk:'ddd'}}}},'a.b.c.{aaa:ooo,bbb}',{aaa:123,bbb:321})
    ==>
    {a:{b:{c:{ooo:123,bbb:321,kkk:'ddd'}}}}
    Path.getIn({a:{b:{c:{ooo:123,bbb:321,kkk:'ddd'}}}},'a.b.c.{aaa:ooo,bbb}')
    ==>
    {aaa:123,bbb:321}
  • Array Pattern

    Path.setIn({},'a.b.c.[aaa,bbb]',[123,321])
    ==>
    {a:{b:{c:{aaa:123,bbb:321}}}}
    Path.getIn({a:{b:{c:{aaa:123,bbb:321}}}},'a.b.c.[aaa,bbb]')
    ==>
    [123,321]
    
    Path.setIn({a:{b:{c:{kkk:'ddd'}}}},'a.b.c.[aaa,bbb]',[123,321])
    ==>
    {a:{b:{c:{aaa:123,bbb:321,kkk:'ddd'}}}}
    Path.getIn({a:{b:{c:{aaa:123,bbb:321,kkk:'ddd'}}}},'a.b.c.[aaa,bbb]')
    ==>
    [123,321]
  • Nested Array and Object Pattern

    Path.setIn({},'a.b.c.[{ddd,kkk:mmm},bbb]',[{ddd:123,kkk:'hhhh'},321])
    ==>
    {a:{b:{c:{ddd:123,bbb:321,mmm:'hhh'}}}}
    Path.getIn({a:{b:{c:{ddd:123,bbb:321,mmm:'hhh'}}}},'a.b.c.[{ddd,kkk:mmm},bbb]')
    ==>
    [{ddd:123,kkk:'hhh'},321]
    
    Path.setIn({a:{b:{c:{kkk:'ddd'}}}},'a.b.c.{aaa:ooo,bbb:[ccc,ddd]}',{aaa:123,bbb:[123,321]})
    ==>
    {a:{b:{c:{ooo:123,ccc:123,ddd:321,kkk:'ddd'}}}}
    Path.getIn({a:{b:{c:{ooo:123,ccc:123,ddd:321,kkk:'ddd'}}}},'a.b.c.{aaa:ooo,bbb:[ccc,ddd]}')
    ==>
    {aaa:123,bbb:[123,321]}

Path Match Pattern Syntax

Wildcard

"*"

Expand String

"aaa~" or "~" or "aaa~.bbb.cc"

Part Wildcard

"a.b.*.c.*"

Wildcard With Group Filter

"a.b.*(aa.bb.dd,cc,mm)"
or 
"a.b.*(!aa.bb.dd,cc,mm)"

Wildcard With Nest Group Filter

"a.b.*(aa.bb.*(aa.b,c),cc,mm)"
or 
"a.b.*(!aa.bb.*(aa.b,c),cc,mm)"

Wildcard With Range Filter

"a.b.*[10:100]"
or 
"a.b.*[10:]"
or 
"a.b.*[:100]"

Ignore Key Word

"a.b.[[cc.uu()sss*\\[1222\\]]]"

LICENSE

The MIT License (MIT)

Copyright (c) 2018 JanryWang

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

redux-callbag

🕺🕺Redux middleware for action side effects with callbag 👉< 1KB
JavaScript
182
star
2

react-study

React源码分析,需要进一步了解请移步https://github.com/purplebamboo/little-reactjs
JavaScript
173
star
3

react-eva

Effects+View+Actions(React distributed state management solution with rxjs.)
JavaScript
156
star
4

doc-scripts

React Document Build Tool Scripts , Like react-scripts of create react app
JavaScript
155
star
5

mfetch

mfetch will provide you with a strong ability to request resource management
JavaScript
93
star
6

awesome-callbags

Callbag Libraries & Learning Material https://github.com/callbag/callbag
86
star
7

callbag-map-promise

Callbag map promise
JavaScript
65
star
8

callbag-delay

callbag delay
JavaScript
60
star
9

immutability-skinable-component

immutability-skinable-component
JavaScript
48
star
10

exp-parser

An expression parser like the angular's parser
JavaScript
41
star
11

kvm

KVM.js 轻量级模块管理器,依赖注入,AMD,Commonjs规范,Promise化
JavaScript
41
star
12

react-propers

Select react doms , and update props.
JavaScript
40
star
13

gub

CLI tool for create an npm package from any repos. 🐳
JavaScript
33
star
14

dot-match

Extremely easy dot path matching tool
JavaScript
29
star
15

qverse

Traverse any data with DPML commands.
JavaScript
25
star
16

immutability

Improve the react components immutability by es6 decorate
JavaScript
14
star
17

react-chrome-boilerplate

Improve your development efficiency to create chrome extension.
JavaScript
9
star
18

github-tag-release

github release by git tags
TypeScript
9
star
19

react-banners

React通用横幅组件
JavaScript
8
star
20

react-snap-svg

react snap.svg
JavaScript
5
star
21

react-stikky

It's very easy to sticky fixed target element.
JavaScript
5
star
22

react-slot-props

slot slot slot , react slot!
JavaScript
5
star
23

react-code-snippet

React Code Snippet
JavaScript
4
star
24

react-site-renderer

JavaScript
3
star
25

awesome-react-suspense

Awesome list of React components with suspense and resources.
3
star
26

react-demo-loader

webpack loader for react demo
JavaScript
2
star
27

dom-imagify

Generates an image from a DOM node using HTML5 canvas
JavaScript
2
star
28

use-element-rect-observer

TypeScript
2
star
29

get-target-cookie

TypeScript
1
star
30

react-doc-renderer

JavaScript
1
star
31

super-plugin

JavaScript
1
star
32

janry.blog.io

1
star
33

clean-ns

Clean Node Modules
JavaScript
1
star
34

janryWang

Config files for my GitHub profile.
1
star
35

deproxy

Downgradable ES Proxy solution
1
star