Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add depth to option #5

Merged
merged 5 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 43 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,57 @@ import {JsonItemSelector} from "json-item-selector";
// CommonJS
const {JsonItemSelector} = require("json-item-selector");

const json = {
"galaxy": {
"milky-way": {
"earth": [
"africa",
"asia",
"aurope",
],
"venus": [
"valnaes",
],
},
"andromeda": {
"pegasus":[],
"cassopia":[]
}
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(json);
const JIS = new JsonItemSelector(galaxy);

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

JIS.select_option("galaxy"); // returns true if successful
JIS.select("milky-way", 0); // --> Selects from options at 0 depth

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

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

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

JIS.select_option("earth");

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

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

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

```
[CodePen Example](https://codepen.io/Yoseph-Tenaw/pen/GRzVZzO)

# Installation
```
Expand All @@ -56,8 +68,10 @@ npm install json-item-selector
# Methods
| Name | Description | Params | Defaults | Returns |
| ------ | ------ | ------ | ------ | ------ |
| list_options | List all the possible options to select, starting from the first level/ highest depth | - | | `string []` |
| select_option | Choose from the available options from the current level/depth, after selecting the next list of option will go to the next level/depth of properties | `option: string` `deny_repeats?: boolean` | `deny_repeats: false`| `boolean` |
| list | List all the possible options to select at a given depth in the object | `depth: number` | | `string []` |
| select | Choose from the available options from the given depth | `option: string` `deny_repeats?: boolean` `depth: number` | `deny_repeats: false`| `boolean` |
| list_no_depth | List all the possible options to select, starting from the first level/ highest depth | - | | `string []` |
| select_no_depth | Choose from the available options from the current level/depth, after selecting the next list of option will go to the next level/depth of properties | `option: string` `deny_repeats?: boolean` | `deny_repeats: false`| `boolean` |
| get_all_selected | Lists all the options selected so far | - | | `string []` |
| get_last_selected | Gets the last selected option | - | | `string` |
| clear_last | Remove the last selected option and go back up a level/higher in depth | - | | `boolean` |
Expand Down
51 changes: 48 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class JsonItemSelector {
this.choice_tree = [];
}

public list_options ():string[] {
public list_no_depth ():string[] {
const option_list: string[] = [];
if (this.choice_tree.length === 0) {
for (const key in this.json) {
Expand Down Expand Up @@ -38,15 +38,60 @@ export class JsonItemSelector {
return option_list;
}

public select_option (option: string, deny_repeats: boolean = false):boolean {
if (!this.list_options().includes(option))
public list (at_depth: number):string[] {
const option_list: string[] = [];
if (at_depth < 0 || at_depth > this.choice_tree.length)
return [];

if (this.choice_tree.length === 0) {
for (const key in this.json) {
if (Object.prototype.hasOwnProperty.call(this.json, key)) {
option_list.push(key);
}
}
}
else {
const next_object = JsonItemSelector.access_value(this.choice_tree.slice(0, at_depth), this.json, 0);
if (Array.isArray(next_object)) {
for(let i = 0; i < next_object.length; i++) {
option_list.push(next_object[i]);
}
}
else if (typeof next_object === "object") {
for (const key in next_object) {
if (Object.prototype.hasOwnProperty.call(next_object, key)) {
option_list.push(key);
}
}
}
else {
option_list.length = 0;
}
}
return option_list;
}

public select_no_depth (option: string, deny_repeats: boolean = false):boolean {
if (!this.list_no_depth().includes(option))
return false;
if (deny_repeats && this.choice_tree.includes(option))
return false;
this.choice_tree.push(option);
return true
}

public select (option: string, at_depth: number, deny_repeats: boolean = false):boolean {
if (!this.list(at_depth).includes(option))
return false;
if (deny_repeats && this.choice_tree.includes(option))
return false;
if (at_depth < 0 || at_depth > this.choice_tree.length)
return false;
this.choice_tree.splice(at_depth);
this.choice_tree[at_depth] = option;
return true
}

public get_all_selected ():string[] {
return this.choice_tree;
}
Expand Down
Loading
Loading