1.1.3 • Published 2 years ago

json-item-selector v1.1.3

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

json-item-selector

Tests

A package that allow you to create choice logic based on a json file. It helps with choice implementations which depend on an external json file and are heavily nested, mostly for use cases like location selection or game dialog mapping. A basic example below

// ESM
import {JsonItemSelector} from "json-item-selector";
// CommonJS
const {JsonItemSelector} = require("json-item-selector");


const galaxy = {
    // Depth: 0
    "milky-way": {
        // Depth: 1
        "earth": [
            // Depth: 2
            "africa",
            "asia",
            "aurope",
        ],
        // Depth: 1
        "venus": [
            // Depth: 2
            "valnaes",
        ],
    },
    // Depth: 0
    "andromeda": {
        // Depth: 1
        "pegasus":[
          // Depth: 2
          "perg",
          "gaort"
        ],
        // Depth: 1
        "cassopia":[
           // Depth: 2
          'epian',
          'siltron'
        ]
    }
}



const JIS = new JsonItemSelector(galaxy);

console.log(JIS.list(0)); // ["milky-way", "andromeda"] --> List options at 0 depth 

JIS.select("milky-way", 0); // --> Selects from options at 0 depth

console.log(JIS.list(1)); // ["earth", "venus"] --> List options at depth 1

JIS.select("earth", 1); // --> Selects from options at depth 1

console.log(JIS.list(2)); // ["africa", "asia", "europe"]

JIS.select("africa", 2);

console.log(JIS.get_all_selected()); // ["milky-way", "earth", "africa"]

CodePen Example

Installation

npm install json-item-selector

Methods

NameDescriptionParamsDefaultsReturns
listList all the possible options to select at a given depth in the objectdepth: numberstring []
selectChoose from the available options from the given depthoption: string deny_repeats?: boolean depth: numberdeny_repeats: falseboolean
list_no_depthList all the possible options to select, starting from the first level/ highest depth-string []
select_no_depthChoose from the available options from the current level/depth, after selecting the next list of option will go to the next level/depth of propertiesoption: string deny_repeats?: booleandeny_repeats: falseboolean
get_all_selectedLists all the options selected so far-string []
get_last_selectedGets the last selected option-string
clear_lastRemove the last selected option and go back up a level/higher in depth-boolean
clear_all_selectedRemove all selection and return to the first level-boolean
1.1.3

2 years ago

1.1.1

2 years ago

1.1.2

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago