yet-another-storage-package 1.0.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.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # YASP (Yet Another Storage Package)
2
+ YASP is a package for storing data.
3
+ ```ts
4
+ import YASP from "yet-another-storage-package"
5
+ const myYASP = new YASP<number>()
6
+
7
+ myYASP.addValue("a", 20)
8
+ .addValue("b", 25)
9
+ .addValue("c", 30)
10
+
11
+ myYASP.getValue("a") // 20
12
+ ```
13
+ Files made with tsc.
package/errors.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "ValueAppending": "Error adding value: Value already exists, use changeValue() instead.",
3
+ "ValueNotFound": "Error finding value: Value does not exist."
4
+ }
package/index.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ declare type YASPConfig = Partial<{
2
+ throwIfNotFound: boolean;
3
+ }>;
4
+ export default class YASP<T> {
5
+ protected data: Record<string, T>;
6
+ protected config: YASPConfig;
7
+ constructor(init?: Record<string, T>);
8
+ get Data(): Record<string, T>;
9
+ setConfig(conf: YASPConfig): this;
10
+ addConfig<K extends keyof YASPConfig>(...configs: [K, YASPConfig[K]][]): this;
11
+ addValue(key: string, value: T): this;
12
+ changeValue(key: string, value: T): this;
13
+ removeValue(key: string): this;
14
+ getValue(key: string): T;
15
+ }
16
+ export {};
package/index.js ADDED
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ exports.__esModule = true;
14
+ var errors = require("./errors.json");
15
+ var warns = require("./warnings.json");
16
+ var defaultYASPConfig = {
17
+ throwIfNotFound: true
18
+ };
19
+ var YASP = /** @class */ (function () {
20
+ function YASP(init) {
21
+ if (init === void 0) { init = {}; }
22
+ this.config = defaultYASPConfig;
23
+ this.data = init;
24
+ }
25
+ Object.defineProperty(YASP.prototype, "Data", {
26
+ get: function () {
27
+ return this.data;
28
+ },
29
+ enumerable: false,
30
+ configurable: true
31
+ });
32
+ YASP.prototype.setConfig = function (conf) {
33
+ this.config = conf;
34
+ return this;
35
+ };
36
+ YASP.prototype.addConfig = function () {
37
+ var configs = [];
38
+ for (var _i = 0; _i < arguments.length; _i++) {
39
+ configs[_i] = arguments[_i];
40
+ }
41
+ for (var _a = 0, configs_1 = configs; _a < configs_1.length; _a++) {
42
+ var konfig = configs_1[_a];
43
+ var kkey = konfig[0], kvalue = konfig[1];
44
+ this.config[kkey] = kvalue;
45
+ }
46
+ return this;
47
+ };
48
+ YASP.prototype.addValue = function (key, value) {
49
+ if (key in this.data) {
50
+ throw new Error(errors.ValueAppending);
51
+ }
52
+ this.data[key] = value;
53
+ return this;
54
+ };
55
+ YASP.prototype.changeValue = function (key, value) {
56
+ if (!(key in this.data)) {
57
+ throw new Error(errors.ValueNotFound);
58
+ }
59
+ this.data[key] = value;
60
+ return this;
61
+ };
62
+ YASP.prototype.removeValue = function (key) {
63
+ var _this = this;
64
+ if (!(key in this.data)) {
65
+ if (this.config.throwIfNotFound) {
66
+ throw new Error(errors.ValueNotFound);
67
+ }
68
+ else {
69
+ console.warn(warns.ValueNotFound);
70
+ }
71
+ }
72
+ this.data = Object.keys(this.data)
73
+ .filter(function (k) { return k !== key; })
74
+ .reduce(function (acc, k) {
75
+ var _a;
76
+ return __assign(__assign({}, acc), (_a = {}, _a[k] = _this.data[k], _a));
77
+ }, {});
78
+ return this;
79
+ };
80
+ YASP.prototype.getValue = function (key) {
81
+ if (!(key in this.data)) {
82
+ if (this.config.throwIfNotFound) {
83
+ throw new Error(errors.ValueNotFound);
84
+ }
85
+ else {
86
+ console.warn(warns.ValueNotFound);
87
+ }
88
+ }
89
+ return this.data[key];
90
+ };
91
+ return YASP;
92
+ }());
93
+ exports["default"] = YASP;
package/index.ts ADDED
@@ -0,0 +1,71 @@
1
+ import * as errors from "./errors.json"
2
+ import * as warns from "./warnings.json"
3
+
4
+ type YASPConfig = Partial<{
5
+ throwIfNotFound: boolean;
6
+ }>
7
+
8
+ const defaultYASPConfig: YASPConfig = {
9
+ throwIfNotFound: true
10
+ }
11
+
12
+ export default class YASP<T> {
13
+ protected data: Record<string, T>;
14
+ protected config: YASPConfig = defaultYASPConfig;
15
+ constructor(init: Record<string, T> = {}) {
16
+ this.data = init
17
+ }
18
+ get Data(): Record<string, T> {
19
+ return this.data
20
+ }
21
+ setConfig(conf: YASPConfig) {
22
+ this.config = conf
23
+ return this
24
+ }
25
+ addConfig<K extends keyof YASPConfig>(...configs: [ K, YASPConfig[ K ] ][]) {
26
+ for (let konfig of configs) {
27
+ const [ kkey, kvalue ] = konfig
28
+ this.config[ kkey ] = kvalue
29
+ }
30
+ return this
31
+ }
32
+ addValue(key: string, value: T) {
33
+ if (key in this.data) {
34
+ throw new Error(errors.ValueAppending)
35
+ }
36
+ this.data[ key ] = value
37
+ return this
38
+ }
39
+ changeValue(key: string, value: T) {
40
+ if (!(key in this.data)) {
41
+ throw new Error(errors.ValueNotFound)
42
+ }
43
+ this.data[ key ] = value
44
+ return this
45
+ }
46
+ removeValue(key: string) {
47
+ if (!(key in this.data)) {
48
+ if (this.config.throwIfNotFound) {
49
+ throw new Error(errors.ValueNotFound)
50
+ } else {
51
+ console.warn(warns.ValueNotFound)
52
+ }
53
+ }
54
+ this.data = Object.keys(this.data)
55
+ .filter(k => k !== key)
56
+ .reduce<Record<string, T>>((acc, k) => {
57
+ return { ...acc, [ k ]: this.data[ k ] }
58
+ }, {})
59
+ return this
60
+ }
61
+ getValue(key: string){
62
+ if (!(key in this.data)) {
63
+ if (this.config.throwIfNotFound) {
64
+ throw new Error(errors.ValueNotFound)
65
+ } else {
66
+ console.warn(warns.ValueNotFound)
67
+ }
68
+ }
69
+ return this.data[key]
70
+ }
71
+ }
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "yet-another-storage-package",
3
+ "version": "1.0.0",
4
+ "description": "YASP is a package for storing data.",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "keywords": [],
8
+ "author": "",
9
+ "license": "MIT",
10
+ "scripts": {
11
+ "test": "echo \"Error: no test specified\" && exit 1"
12
+ }
13
+ }
package/warnings.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "ValueNotFound": "WARNING: Value not found, returning undefined..."
3
+ }