horae-configure 0.0.7 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +73 -25
  2. package/dist/horae.js +1 -1
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,83 +1,131 @@
1
1
  # Introduction
2
2
 
3
- "horae" is a lightweight JavaScript library that provides methods to easily read JSON files and
4
- manipulate data using the `set`, `get`, `has`, and `save` functions.
3
+ "horae" is a lightweight TypeScript library that provides methods to easily read JSON files and manipulate data using dot-notation and array index property access.
5
4
 
6
5
  ## Features
7
6
 
8
7
  - Read JSON files effortlessly.
9
- - Modify JSON data using `set` and `get` methods.
8
+ - Get and set nested properties using dot-notation (e.g. `position.x`) or array index syntax (e.g. `items[0].name`).
10
9
  - Check if a specific property exists using `has`.
11
- - Save the updated JSON data back to the file.
10
+ - Delete properties with `delete`.
11
+ - Reset all data with `clear`.
12
+ - Reload data from file with `reload`.
13
+ - Safe property access with `getOrDefault`.
14
+ - Save updated data back to the file with `save`.
12
15
 
13
16
  ## Installation
14
17
 
15
- You can install "horae" via npm:
16
-
17
18
  ```bash
18
19
  npm install horae-configure
19
20
  ```
20
21
 
21
- You can install "horae" via yarn:
22
-
23
22
  ```bash
24
23
  yarn add horae-configure
25
24
  ```
26
25
 
27
26
  # How to use?
28
27
 
29
- You need to first prepare a JSON file in the outermost layer of the project
28
+ Prepare a JSON file in the root of your project:
30
29
 
31
30
  ```json
32
31
  {
33
32
  "position": {
34
33
  "x": 1,
35
34
  "y": 2
36
- }
35
+ },
36
+ "items": [
37
+ { "name": "foo" }
38
+ ]
37
39
  }
38
40
  ```
39
41
 
40
- and then initialize `horae` variable
42
+ Initialize a `Horae` instance with the filename (without extension):
41
43
 
42
44
  ```typescript
45
+ import { Horae } from 'horae-configure';
46
+
43
47
  const horae = new Horae<{
44
- position: {
45
- x: number;
46
- y: number;
47
- };
48
+ position: { x: number; y: number };
49
+ items: { name: string }[];
48
50
  }>('config');
49
51
  ```
50
52
 
51
- ### `get` method
53
+ ### `get`
54
+
55
+ Retrieve a value using dot-notation or array index syntax.
52
56
 
53
57
  ```typescript
54
- horae.get('position.x'); // 1
55
- horae.get('position.y'); // 2
58
+ horae.get('position.x'); // 1
59
+ horae.get('items[0].name'); // "foo"
60
+ horae.get('position.z'); // undefined
56
61
  ```
57
62
 
58
- ### `has` method
63
+ ### `has`
64
+
65
+ Check whether a property exists.
59
66
 
60
67
  ```typescript
61
68
  horae.has('position.x'); // true
62
- horae.has('position.y'); // true
63
69
  horae.has('position.z'); // false
64
70
  ```
65
71
 
66
- ### `set` method
72
+ ### `set`
73
+
74
+ Set a value at a given path. Intermediate objects are created automatically.
67
75
 
68
76
  ```typescript
69
77
  horae.set('position.x', 100);
70
- horae.get('position.x'); // 100
78
+ horae.set('profile.address.city', 'Taipei');
79
+ ```
80
+
81
+ ### `delete`
82
+
83
+ Remove a property at a given path.
84
+
85
+ ```typescript
86
+ horae.delete('position.x');
87
+ horae.has('position.x'); // false
88
+ horae.has('position.y'); // true
89
+ ```
90
+
91
+ ### `clear`
92
+
93
+ Reset all data to an empty object.
94
+
95
+ ```typescript
96
+ horae.clear();
97
+ horae.has('position'); // false
71
98
  ```
72
99
 
73
- ### `save` method
100
+ ### `reload`
101
+
102
+ Discard in-memory changes and re-read data from the file.
103
+
104
+ ```typescript
105
+ horae.set('position.x', 999);
106
+ horae.reload();
107
+ horae.get('position.x'); // back to original value from file
108
+ ```
109
+
110
+ ### `getOrDefault`
111
+
112
+ Return the value if it exists, otherwise return the provided default.
113
+
114
+ ```typescript
115
+ horae.getOrDefault('position.x', 0); // 1
116
+ horae.getOrDefault('position.z', 0); // 0
117
+ ```
118
+
119
+ ### `save`
120
+
121
+ Persist the current in-memory data back to the JSON file.
74
122
 
75
123
  ```typescript
76
- horae.set('position.', 100);
124
+ horae.set('position.x', 1000);
77
125
  horae.save();
78
126
  ```
79
127
 
80
- Go back and view the JSON file
128
+ The file will be updated to:
81
129
 
82
130
  ```json
83
131
  {
package/dist/horae.js CHANGED
@@ -1 +1 @@
1
- import{createRequire as t}from"module";var n,e={n:t=>{var n=t&&t.__esModule?()=>t.default:()=>t;return e.d(n,{a:n}),n},d:(t,n)=>{for(var r in n)e.o(n,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:n[r]})},o:(t,n)=>Object.prototype.hasOwnProperty.call(t,n)},r={};e.d(r,{d:()=>s}),function(t){t.JSON="json"}(n||(n={}));const o=t(import.meta.url)("fs");var i=e.n(o),a=function(t){return t.split(".").filter(Boolean)};function c(t){return c="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},c(t)}function f(t,n){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!e){if(Array.isArray(t)||(e=function(t,n){if(t){if("string"==typeof t)return u(t,n);var e=Object.prototype.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?u(t,n):void 0}}(t))||n&&t&&"number"==typeof t.length){e&&(t=e);var r=0,o=function(){};return{s:o,n:function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,a=!0,c=!1;return{s:function(){e=e.call(t)},n:function(){var t=e.next();return a=t.done,t},e:function(t){c=!0,i=t},f:function(){try{a||null==e.return||e.return()}finally{if(c)throw i}}}}function u(t,n){(null==n||n>t.length)&&(n=t.length);for(var e=0,r=new Array(n);e<n;e++)r[e]=t[e];return r}function l(t,n){for(var e=0;e<n.length;e++){var r=n[e];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,(void 0,o=function(t,n){if("object"!==c(t)||null===t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var r=e.call(t,"string");if("object"!==c(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(r.key),"symbol"===c(o)?o:String(o)),r)}var o}var s=function(){function t(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:n.JSON;!function(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")}(this,t),Object.defineProperty(this,"config",{enumerable:!0,configurable:!0,writable:!0,value:{name:"config"}}),this.config.name=e,this.config.type=r,this.initialize()}var e,r;return e=t,(r=[{key:"initialize",value:function(){var t="".concat(this.config.name,".").concat(this.config.type);if(i().existsSync(t)){var n=JSON.parse(i().readFileSync("".concat(this.config.name,".").concat(this.config.type),"utf8"));this.config.data=n}else this.config.data={}}},{key:"set",value:function(t,n){var e=a(t),r=Object.assign({},this.config.data);e.reduce((function(t,r){return r===e[e.length-1]?t[r]=n:t[r]||(t[r]={}),t[r]}),r),this.config.data=r}},{key:"get",value:function(t){if(this.config.data){var n,e=this.config.data,r=f(a(t));try{for(r.s();!(n=r.n()).done;){var o=n.value;if(!e.hasOwnProperty(o))return;e=e[o]}}catch(t){r.e(t)}finally{r.f()}return e}}},{key:"has",value:function(t){if(!this.config.data)return!1;var n,e=this.config.data,r=f(a(t));try{for(r.s();!(n=r.n()).done;){var o=n.value;if(!e.hasOwnProperty(o))return!1;e=e[o]}}catch(t){r.e(t)}finally{r.f()}return!0}},{key:"save",value:function(){var t="".concat(this.config.name,".").concat(this.config.type);i().writeFileSync(t,JSON.stringify(this.config.data,null,2))}}])&&l(e.prototype,r),Object.defineProperty(e,"prototype",{writable:!1}),t}(),y=r.d;export{y as Horae};
1
+ import{createRequire as t}from"module";var e,n={n:t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},d:(t,e)=>{for(var r in e)n.o(e,r)&&!n.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},r={};n.d(r,{d:()=>s}),function(t){t.JSON="json"}(e||(e={}));const o=t(import.meta.url)("fs");var i=n.n(o),a=function(t){return t.split(".").flatMap((function(t){return t.split(/\[(\d+)\]/).filter(Boolean)})).filter(Boolean)};function c(t){return c="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},c(t)}function f(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!n){if(Array.isArray(t)||(n=function(t,e){if(t){if("string"==typeof t)return u(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?u(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var r=0,o=function(){};return{s:o,n:function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,a=!0,c=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return a=t.done,t},e:function(t){c=!0,i=t},f:function(){try{a||null==n.return||n.return()}finally{if(c)throw i}}}}function u(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function l(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,(void 0,o=function(t,e){if("object"!==c(t)||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,"string");if("object"!==c(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(r.key),"symbol"===c(o)?o:String(o)),r)}var o}var s=function(){function t(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:e.JSON;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),Object.defineProperty(this,"config",{enumerable:!0,configurable:!0,writable:!0,value:{name:"config"}}),this.config.name=n,this.config.type=r,this.initialize()}var n,r;return n=t,(r=[{key:"initialize",value:function(){var t="".concat(this.config.name,".").concat(this.config.type);if(i().existsSync(t)){var e=JSON.parse(i().readFileSync("".concat(this.config.name,".").concat(this.config.type),"utf8"));this.config.data=e}else this.config.data={}}},{key:"set",value:function(t,e){var n=a(t),r=Object.assign({},this.config.data);n.reduce((function(t,r){return r===n[n.length-1]?t[r]=e:t[r]||(t[r]={}),t[r]}),r),this.config.data=r}},{key:"get",value:function(t){if(this.config.data){var e,n=this.config.data,r=f(a(t));try{for(r.s();!(e=r.n()).done;){var o=e.value;if("object"!==c(n)||null===n||!Object.prototype.hasOwnProperty.call(n,o))return;n=n[o]}}catch(t){r.e(t)}finally{r.f()}return n}}},{key:"has",value:function(t){if(!this.config.data)return!1;var e,n=this.config.data,r=f(a(t));try{for(r.s();!(e=r.n()).done;){var o=e.value;if("object"!==c(n)||null===n||!Object.prototype.hasOwnProperty.call(n,o))return!1;n=n[o]}}catch(t){r.e(t)}finally{r.f()}return!0}},{key:"delete",value:function(t){var e=a(t),n=Object.assign({},this.config.data);e.reduce((function(t,n,r){return r===e.length-1?(delete t[n],t):t[n]}),n),this.config.data=n}},{key:"clear",value:function(){this.config.data={}}},{key:"reload",value:function(){this.initialize()}},{key:"getOrDefault",value:function(t,e){var n=this.get(t);return void 0!==n?n:e}},{key:"save",value:function(){var t="".concat(this.config.name,".").concat(this.config.type);i().writeFileSync(t,JSON.stringify(this.config.data,null,2))}}])&&l(n.prototype,r),Object.defineProperty(n,"prototype",{writable:!1}),t}(),y=r.d;export{y as Horae};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "horae-configure",
3
3
  "private": false,
4
- "version": "0.0.7",
4
+ "version": "0.1.0",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"