Skip to content

Commit

Permalink
1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
acegoal07 committed Jul 17, 2022
1 parent 17c8bc3 commit bdd6565
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ file.set("data", []);
console.log(file.get("Hello"))
// log => world!

file.append("data", "test");
file.append("data", "test2");
file.push("data", "test");
file.push("data", "test2")
file.unshift("data", "test3");
file.set("items", {"world": "world!"});
file.unset("Hello");
// {
// "data": [
// "test3",
// "test",
// "test2"
// ],
Expand Down Expand Up @@ -280,8 +282,22 @@ Remove a path from a JSON object


<!-- append !-->
### `.append(path, value)`
Appends a value/object to a specific path, If the path is empty it wil create a list
### `.unshift(path, value)`
Pushes the data to the top of the specified array
<h4>Params</h4>

- `path` - The object path
- `value` - The data you want to put into the json file
<h4>Return</h4>

- Returns an instance of the `JsonEditor`
<br>



<!-- append !-->
### `.push(path, value)`
Pushes the data to the bottom of the specified array
<h4>Params</h4>

- `path` - The object path
Expand Down
45 changes: 41 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,30 @@ class editor {
}

/**
* Appends a value/object to a specific path
* If the path is empty it wil create a list
*
* @deprecated This feature has been deprecated in favour of adding pushTop and pushBottom
*/
append(path, value) {
if (!path) {
throw new Error("You did not provide a path")
}
let data = this.get(path);
data = (data === undefined) ? [] : data;
if (!Array.isArray(data)) {
throw new Error("The data is not an array!");
}
data.push(value);
this.set(path,data);
return this;
}

/**
* Pushes the data to the top of the specified array
*
* @param {String} path The object path
* @param {Anything} value The value
* @returns {JsonEditor} The `JsonEditor` instance
*/
append(path, value) {
push(path, value) {
if (!path) {
throw new Error("You did not provide a path")
}
Expand All @@ -234,6 +250,27 @@ class editor {
return this;
}

/**
* Pushes the data to the bottom of the specified array
*
* @param {String} path The object path
* @param {Anything} value The value
* @returns {JsonEditor} The `JsonEditor` instance
*/
unshift(path, value) {
if (!path) {
throw new Error("You did not provide a path")
}
let data = this.get(path);
data = (data === undefined) ? [] : data;
if (!Array.isArray(data)) {
throw new Error("The data is not an array!");
}
data.unshift(value);
this.set(path,data);
return this;
}

/**
* Remove the last item from an array
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@acegoal07/json-editor",
"version": "1.0.2",
"version": "1.0.3",
"description": "A simple json file editor",
"main": "index",
"repository": {
Expand Down

0 comments on commit bdd6565

Please sign in to comment.