Coder Social home page Coder Social logo

hammy2899 / dot-notation-tokenizer Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 207 KB

Dot notation tokenizer

Home Page: https://www.npmjs.com/package/dot-notation-tokenizer

License: MIT License

JavaScript 6.61% TypeScript 93.39%
javascript tokenizer

dot-notation-tokenizer's Introduction

dot-notation-tokenizer

Convert a dot notation string into a tokenized array.


GitHub Workflow Status Coverage Status npm JavaScript Style Guide License

Installation

yarn

yarn add dot-notation-tokenizer

npm

npm i dot-notation-tokenizer

Usage

JavaScript ES / TypeScript

import { tokenize } from 'dot-notation-tokenizer'

tokenize('dot.notation')

Browser

<script src="path/to/script/index.min.js"></script>

<script>
  dotNotationTokenizer.tokenize('dot.notation')
</script>

NodeJS

const { tokenize } = require('dot-notation-tokenizer')

tokenize('dot.notation')

Extra exported functions and types

import {
  notationFromTokens,     // convert tokens array (returned from tokenize) into a dot notation string
  isNotationToken,        // check if a value is a valid token
  tokensFromPropertyKeys, // convert an array of property keys into an array of tokens
  escapeProperty,         // escape token characters
  unescapeProperty,       // remove \ from escaped token characters
  Tokens                  // this is actually a class which extends Array and contains the _notation property
} from 'dot-notation-tokenizer'
// Most of these functions are used internally however they can also prove useful

import type {
  TokenKind,        // either 'PROPERTY' or 'ARRAY_INDEX'
  PropertyToken,    // interface with properties for a property token
  ArrayIndexToken,  // interface with properties for an array index token
  Token             // either PropertyToken or ArrayIndexToken
} from 'dot-notation-tokenizer'

Explanation

Dot notation can consist of basic property keys seperated by a . and also array indexes within [x] like the following.

path.to.array[1]

The above notation would return the following token array.

[
  {
    "kind": "PROPERTY",
    "value": "path",
    "index": {
      "start": 0,
      "end": 4
    },
    "escaped": "path"
  },
  {
    "kind": "PROPERTY",
    "value": "to",
    "index": {
      "start": 5,
      "end": 7
    },
    "escaped": "to"
  },
  {
    "kind": "PROPERTY",
    "value": "array",
    "index": {
      "start": 8,
      "end": 13
    },
    "escaped": "array"
  },
  {
    "kind": "ARRAY_INDEX",
    "value": 1,
    "index": {
      "start": 14,
      "end": 16
    },
    "text": "[1]"
  }
]

The notation used above would be used to access the second array value in the following object.

The value returned would be 2.

{
  "path": {
    "to": {
      "array": [
        1,
        2,
        3
      ]
    }
  }
}

You can also chain array indexes for nested arrays like the following notation.

path.to.deep.array[1][0][4]

Escaping characters

If you need to include ., [ or ] in your array key you can simply escape it by putting a slash \ in front of the character.

escaped\\.property\\[0\\].withindex

The above notation would return the following tokens.

[
  {
    "kind": "PROPERTY",
    "value": "escaped.property[0]",
    "index": {
      "start": 0,
      "end": 22
    },
    "escaped": "escaped\\.property\\[0\\]"
  },
  {
    "kind": "PROPERTY",
    "value": "withindex",
    "index": {
      "start": 23,
      "end": 32
    },
    "escaped": "withindex"
  }
]

dot-notation-tokenizer's People

Contributors

y0hami avatar

Watchers

 avatar  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.