Skip to content

Commit bdd6565

Browse files
committed
1.0.3
1 parent 17c8bc3 commit bdd6565

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ file.set("data", []);
4242
console.log(file.get("Hello"))
4343
// log => world!
4444

45-
file.append("data", "test");
46-
file.append("data", "test2");
45+
file.push("data", "test");
46+
file.push("data", "test2")
47+
file.unshift("data", "test3");
4748
file.set("items", {"world": "world!"});
4849
file.unset("Hello");
4950
// {
5051
// "data": [
52+
// "test3",
5153
// "test",
5254
// "test2"
5355
// ],
@@ -280,8 +282,22 @@ Remove a path from a JSON object
280282

281283

282284
<!-- append !-->
283-
### `.append(path, value)`
284-
Appends a value/object to a specific path, If the path is empty it wil create a list
285+
### `.unshift(path, value)`
286+
Pushes the data to the top of the specified array
287+
<h4>Params</h4>
288+
289+
- `path` - The object path
290+
- `value` - The data you want to put into the json file
291+
<h4>Return</h4>
292+
293+
- Returns an instance of the `JsonEditor`
294+
<br>
295+
296+
297+
298+
<!-- append !-->
299+
### `.push(path, value)`
300+
Pushes the data to the bottom of the specified array
285301
<h4>Params</h4>
286302

287303
- `path` - The object path

index.js

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,30 @@ class editor {
213213
}
214214

215215
/**
216-
* Appends a value/object to a specific path
217-
* If the path is empty it wil create a list
218-
*
216+
* @deprecated This feature has been deprecated in favour of adding pushTop and pushBottom
217+
*/
218+
append(path, value) {
219+
if (!path) {
220+
throw new Error("You did not provide a path")
221+
}
222+
let data = this.get(path);
223+
data = (data === undefined) ? [] : data;
224+
if (!Array.isArray(data)) {
225+
throw new Error("The data is not an array!");
226+
}
227+
data.push(value);
228+
this.set(path,data);
229+
return this;
230+
}
231+
232+
/**
233+
* Pushes the data to the top of the specified array
234+
*
219235
* @param {String} path The object path
220236
* @param {Anything} value The value
221237
* @returns {JsonEditor} The `JsonEditor` instance
222238
*/
223-
append(path, value) {
239+
push(path, value) {
224240
if (!path) {
225241
throw new Error("You did not provide a path")
226242
}
@@ -234,6 +250,27 @@ class editor {
234250
return this;
235251
}
236252

253+
/**
254+
* Pushes the data to the bottom of the specified array
255+
*
256+
* @param {String} path The object path
257+
* @param {Anything} value The value
258+
* @returns {JsonEditor} The `JsonEditor` instance
259+
*/
260+
unshift(path, value) {
261+
if (!path) {
262+
throw new Error("You did not provide a path")
263+
}
264+
let data = this.get(path);
265+
data = (data === undefined) ? [] : data;
266+
if (!Array.isArray(data)) {
267+
throw new Error("The data is not an array!");
268+
}
269+
data.unshift(value);
270+
this.set(path,data);
271+
return this;
272+
}
273+
237274
/**
238275
* Remove the last item from an array
239276
*

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@acegoal07/json-editor",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "A simple json file editor",
55
"main": "index",
66
"repository": {

0 commit comments

Comments
 (0)