@based/schema 3.1.0 → 3.2.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 (61) hide show
  1. package/README.md +1 -20
  2. package/dist/display/index.d.ts +2 -0
  3. package/dist/display/index.js +26 -0
  4. package/dist/display/number.d.ts +3 -0
  5. package/dist/display/number.js +89 -0
  6. package/dist/display/string.d.ts +3 -0
  7. package/dist/display/string.js +23 -0
  8. package/dist/display/timestamp.d.ts +3 -0
  9. package/dist/display/timestamp.js +127 -0
  10. package/dist/error.d.ts +19 -0
  11. package/dist/error.js +24 -0
  12. package/dist/index.d.ts +5 -0
  13. package/dist/index.js +22 -0
  14. package/dist/languages.d.ts +187 -0
  15. package/dist/languages.js +190 -0
  16. package/dist/set/fields/array.d.ts +2 -0
  17. package/dist/set/fields/array.js +123 -0
  18. package/dist/set/fields/index.d.ts +3 -0
  19. package/dist/set/fields/index.js +74 -0
  20. package/dist/set/fields/number.d.ts +4 -0
  21. package/dist/set/fields/number.js +129 -0
  22. package/dist/set/fields/object.d.ts +3 -0
  23. package/dist/set/fields/object.js +33 -0
  24. package/dist/set/fields/references.d.ts +3 -0
  25. package/dist/set/fields/references.js +128 -0
  26. package/dist/set/fields/set.d.ts +2 -0
  27. package/dist/set/fields/set.js +63 -0
  28. package/dist/set/fields/string.d.ts +3 -0
  29. package/dist/set/fields/string.js +284 -0
  30. package/dist/set/index.d.ts +3 -0
  31. package/dist/set/index.js +183 -0
  32. package/dist/set/isValidId.d.ts +2 -0
  33. package/dist/set/isValidId.js +21 -0
  34. package/dist/set/types.d.ts +0 -0
  35. package/dist/set/types.js +1 -0
  36. package/dist/src/compat/index.js +10 -2
  37. package/dist/src/compat/newToOld.d.ts +2 -2
  38. package/dist/src/compat/newToOld.js +173 -31
  39. package/dist/src/compat/oldToNew.d.ts +1 -1
  40. package/dist/src/compat/oldToNew.js +200 -25
  41. package/dist/src/generateQuery.d.ts +12 -0
  42. package/dist/src/generateQuery.js +75 -0
  43. package/dist/src/set/fields/references.js +40 -0
  44. package/dist/test/compat.js +25 -0
  45. package/dist/test/query.d.ts +1 -0
  46. package/dist/test/query.js +93 -0
  47. package/dist/types.d.ts +205 -0
  48. package/dist/types.js +27 -0
  49. package/dist/updateSchema.d.ts +2 -0
  50. package/dist/updateSchema.js +16 -0
  51. package/dist/validateSchema.d.ts +4 -0
  52. package/dist/validateSchema.js +41 -0
  53. package/dist/walker/args.d.ts +36 -0
  54. package/dist/walker/args.js +162 -0
  55. package/dist/walker/index.d.ts +6 -0
  56. package/dist/walker/index.js +49 -0
  57. package/dist/walker/parse.d.ts +3 -0
  58. package/dist/walker/parse.js +186 -0
  59. package/dist/walker/types.d.ts +45 -0
  60. package/dist/walker/types.js +10 -0
  61. package/package.json +1 -1
package/README.md CHANGED
@@ -1,21 +1,2 @@
1
- # @based/functions
1
+ # @based/schema
2
2
 
3
- To be used with based cloud functions, adds types and utilities.
4
-
5
- - Example based function
6
-
7
- ```typescript
8
- import { BasedFunction } from '@based/functions'
9
-
10
- const submitVote: BasedFunction<
11
- { target: number },
12
- 'ok' | 'not-ok'
13
- > = async (based, { target }) => {
14
- if (target > 10) {
15
- return 'ok'
16
- }
17
- return 'not-ok'
18
- }
19
-
20
- export default submitVote
21
- ```
@@ -0,0 +1,2 @@
1
+ import { BasedSchemaField } from '../types';
2
+ export declare const display: (value: string | number | void, field: BasedSchemaField) => string | number | void;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.display = void 0;
7
+ const timestamp_1 = __importDefault(require("./timestamp"));
8
+ const number_1 = __importDefault(require("./number"));
9
+ const string_1 = __importDefault(require("./string"));
10
+ const display = (value, field) => {
11
+ if (field.type === 'timestamp' && typeof value === 'number') {
12
+ // @ts-ignore
13
+ return (0, timestamp_1.default)(value, field.display);
14
+ }
15
+ if (field.type === 'number' && typeof value === 'number') {
16
+ // @ts-ignore
17
+ return (0, number_1.default)(value, field.display);
18
+ }
19
+ if (field.type === 'string' && typeof value === 'string') {
20
+ // @ts-ignore
21
+ return (0, string_1.default)(value, field.display);
22
+ }
23
+ return value;
24
+ };
25
+ exports.display = display;
26
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ export type NumberFormat = 'short' | 'human' | 'ratio' | 'bytes' | 'euro' | 'dollar' | 'pound' | `round-${number}`;
2
+ declare const parseNumber: (nr: number | string, format?: NumberFormat) => string | number;
3
+ export default parseNumber;
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const parseNumber = (nr, format) => {
4
+ if (!format) {
5
+ return nr;
6
+ }
7
+ if (typeof nr === 'number') {
8
+ if (format === 'euro' || format === 'dollar' || format === 'pound') {
9
+ const p = String(parseNumber(nr, 'short'));
10
+ const fraction = format === 'euro' ? ',' : '.';
11
+ const [s, f] = p.split('.');
12
+ return `${format === 'euro'
13
+ ? '€'
14
+ : format === 'pound'
15
+ ? '£'
16
+ : format === 'dollar'
17
+ ? '$'
18
+ : ''}${s}${f ? `${fraction}${f}` : ''}`;
19
+ }
20
+ else if (format.startsWith('round-')) {
21
+ const [, fixed] = format.split('round-');
22
+ const n = Number(fixed);
23
+ if (!n) {
24
+ return `${nr.toFixed()}`;
25
+ }
26
+ return `${nr.toFixed(n)}`;
27
+ }
28
+ else if (format === 'bytes') {
29
+ const kb = nr / 1024;
30
+ const mb = kb / 1024;
31
+ const gb = mb / 1024;
32
+ if (gb >= 1) {
33
+ return `${gb.toFixed(2)} gb`;
34
+ }
35
+ else if (mb >= 1) {
36
+ return `${mb.toFixed(2)} mb`;
37
+ }
38
+ else if (kb >= 1) {
39
+ return `${kb.toFixed(2)} kb`;
40
+ }
41
+ else {
42
+ return `${nr} ${nr === 1 ? 'byte' : 'bytes'}`;
43
+ }
44
+ }
45
+ else if (format === 'ratio') {
46
+ return `${Math.round(nr * 10000) / 100}%`;
47
+ }
48
+ else if (format === 'short') {
49
+ if (nr >= 10e9) {
50
+ nr = nr / 1e9;
51
+ nr = nr.toFixed(1);
52
+ if (nr[nr.length - 1] === '0') {
53
+ nr = nr.slice(0, -2);
54
+ }
55
+ return nr + 'b';
56
+ }
57
+ else if (nr >= 10e6) {
58
+ nr = nr / 1e6;
59
+ nr = nr.toFixed(1);
60
+ if (nr[nr.length - 1] === '0') {
61
+ nr = nr.slice(0, -2);
62
+ }
63
+ return nr + 'm';
64
+ }
65
+ else if (nr >= 10e3) {
66
+ nr = nr / 1e3;
67
+ nr = nr.toFixed(1);
68
+ if (nr[nr.length - 1] === '0') {
69
+ nr = nr.slice(0, -2);
70
+ }
71
+ return nr + 'k';
72
+ }
73
+ nr = nr.toFixed(2);
74
+ if (nr[nr.length - 1] === '0') {
75
+ nr = nr.slice(0, -3);
76
+ }
77
+ return String(nr);
78
+ }
79
+ else if (format === 'human') {
80
+ return nr.toFixed(2);
81
+ }
82
+ return String(nr);
83
+ }
84
+ else {
85
+ return nr;
86
+ }
87
+ };
88
+ exports.default = parseNumber;
89
+ //# sourceMappingURL=number.js.map
@@ -0,0 +1,3 @@
1
+ export type StringFormat = 'lowercase' | 'uppercase' | 'capitalize';
2
+ declare const parseString: (value?: string, format?: StringFormat) => string;
3
+ export default parseString;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const parseString = (value, format) => {
4
+ if (!format) {
5
+ return value;
6
+ }
7
+ if (!value) {
8
+ return value;
9
+ }
10
+ if (format === 'lowercase') {
11
+ return value.toLowerCase();
12
+ }
13
+ if (format === 'uppercase') {
14
+ return value.toUpperCase();
15
+ }
16
+ if (format === 'capitalize') {
17
+ const a = value[0];
18
+ return a.toUpperCase() + (value ? value.slice(1) : '');
19
+ }
20
+ return value;
21
+ };
22
+ exports.default = parseString;
23
+ //# sourceMappingURL=string.js.map
@@ -0,0 +1,3 @@
1
+ export type DateFormat = 'date' | 'date-time' | 'date-time-text' | 'human' | 'time' | 'time-precise';
2
+ declare const _default: (nr: number | string, format?: DateFormat) => string | number;
3
+ export default _default;
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const addZero = (d) => {
4
+ const s = d + '';
5
+ if (s.length === 1) {
6
+ return '0' + s;
7
+ }
8
+ return s;
9
+ };
10
+ exports.default = (nr, format) => {
11
+ if (!format) {
12
+ return nr;
13
+ }
14
+ if (typeof nr === 'number') {
15
+ const thenDate = new Date(nr);
16
+ if (format === 'date') {
17
+ return (thenDate.getDate() +
18
+ '/' +
19
+ (thenDate.getMonth() + 1) +
20
+ '/' +
21
+ thenDate.getFullYear());
22
+ }
23
+ else if (format === 'time') {
24
+ return thenDate.getHours() + ':' + addZero(thenDate.getMinutes());
25
+ }
26
+ else if (format === 'time-precise') {
27
+ return (thenDate.getHours() +
28
+ ':' +
29
+ addZero(thenDate.getMinutes()) +
30
+ ':' +
31
+ addZero(thenDate.getSeconds()));
32
+ }
33
+ else if (format === 'date-time-text') {
34
+ return `${thenDate.toLocaleDateString('default', {
35
+ day: 'numeric',
36
+ month: 'long',
37
+ year: 'numeric',
38
+ })}, ${thenDate.toLocaleTimeString()}`;
39
+ }
40
+ else if (format === 'date-time') {
41
+ return (thenDate.getHours() +
42
+ ':' +
43
+ addZero(thenDate.getMinutes()) +
44
+ ' ' +
45
+ thenDate.getDate() +
46
+ '/' +
47
+ (thenDate.getMonth() + 1) +
48
+ '/' +
49
+ thenDate.getFullYear());
50
+ }
51
+ else if (format === 'human') {
52
+ const nowDate = new Date();
53
+ if (nowDate.getTime() > thenDate.getTime()) {
54
+ var secondsSince = Math.floor((nowDate.getTime() - thenDate.getTime()) / 1000);
55
+ var interval = secondsSince / 31536000; // seconds in a year
56
+ if (interval > 1) {
57
+ const x = Math.floor(interval);
58
+ return `${x} ${x > 1 ? 'years' : 'year'} ago`;
59
+ }
60
+ interval = secondsSince / 2592000; // seconds in a month
61
+ if (interval > 1) {
62
+ const x = Math.floor(interval);
63
+ return `${x} ${x > 1 ? 'months' : 'month'} ago`;
64
+ }
65
+ interval = secondsSince / 86400; // seconds in a day
66
+ if (interval > 1) {
67
+ const x = Math.floor(interval);
68
+ return `${x} ${x > 1 ? 'days' : 'day'} ago`;
69
+ }
70
+ interval = secondsSince / 3600; // seconds in an hour
71
+ if (interval > 1) {
72
+ const x = Math.floor(interval);
73
+ return `${x} ${x > 1 ? 'hours' : 'hour'} ago`;
74
+ }
75
+ interval = secondsSince / 60; // seconds in a minute :)
76
+ if (interval > 1) {
77
+ const x = Math.floor(interval);
78
+ return `${x} ${x > 1 ? 'minutes' : 'minute'} ago`;
79
+ }
80
+ if (secondsSince > 10) {
81
+ const x = Math.floor(secondsSince);
82
+ return `${x} ${x > 1 ? 'seconds' : 'second'} ago`;
83
+ }
84
+ return 'Now';
85
+ }
86
+ else {
87
+ // in the future
88
+ var secondsSince = Math.floor((thenDate.getTime() - nowDate.getTime()) / 1000);
89
+ var interval = secondsSince / 31536000; // seconds in a year
90
+ if (interval > 1) {
91
+ const x = Math.floor(interval);
92
+ return `${x} ${x > 1 ? 'years' : 'year'} from now`;
93
+ }
94
+ interval = secondsSince / 2592000; // seconds in a month
95
+ if (interval > 1) {
96
+ const x = Math.floor(interval);
97
+ return `${x} ${x > 1 ? 'months' : 'month'} from now`;
98
+ }
99
+ interval = secondsSince / 86400; // seconds in a day
100
+ if (interval > 1) {
101
+ const x = Math.floor(interval);
102
+ return `${x} ${x > 1 ? 'days' : 'day'} from now`;
103
+ }
104
+ interval = secondsSince / 3600; // seconds in an hour
105
+ if (interval > 1) {
106
+ const x = Math.floor(interval);
107
+ return `${x} ${x > 1 ? 'hours' : 'hour'} from now`;
108
+ }
109
+ interval = secondsSince / 60; // seconds in a minute :)
110
+ if (interval > 1) {
111
+ const x = Math.floor(interval);
112
+ return `${x} ${x > 1 ? 'minutes' : 'minute'} from now`;
113
+ }
114
+ if (secondsSince > 10) {
115
+ const x = Math.floor(secondsSince);
116
+ return `${x} ${x > 1 ? 'seconds' : 'second'} from now`;
117
+ }
118
+ return 'Now';
119
+ }
120
+ }
121
+ return String(nr);
122
+ }
123
+ else {
124
+ return nr;
125
+ }
126
+ };
127
+ //# sourceMappingURL=timestamp.js.map
@@ -0,0 +1,19 @@
1
+ export declare enum ParseError {
2
+ 'incorrectFieldType' = 0,
3
+ 'incorrectNodeType' = 1,
4
+ 'exceedsMaximum' = 2,
5
+ 'subceedsMinimum' = 3,
6
+ 'fieldDoesNotExist' = 4,
7
+ 'incorrectFormat' = 5,
8
+ 'referenceIsIncorrectType' = 6,
9
+ 'valueAndDefault' = 7,
10
+ 'defaultNotSupported' = 8,
11
+ 'multipleOperationsNotAllowed' = 9,
12
+ 'requiredFieldNotDefined' = 10,
13
+ 'languageNotSupported' = 11,
14
+ 'invalidJSON' = 12,
15
+ 'noLanguageFound' = 13,
16
+ 'cannotDeleteNodeFromModify' = 14,
17
+ 'nestedModifyObjectNotAllowed' = 15,
18
+ 'infinityNotSupported' = 16
19
+ }
package/dist/error.js ADDED
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ParseError = void 0;
4
+ var ParseError;
5
+ (function (ParseError) {
6
+ ParseError[ParseError["incorrectFieldType"] = 0] = "incorrectFieldType";
7
+ ParseError[ParseError["incorrectNodeType"] = 1] = "incorrectNodeType";
8
+ ParseError[ParseError["exceedsMaximum"] = 2] = "exceedsMaximum";
9
+ ParseError[ParseError["subceedsMinimum"] = 3] = "subceedsMinimum";
10
+ ParseError[ParseError["fieldDoesNotExist"] = 4] = "fieldDoesNotExist";
11
+ ParseError[ParseError["incorrectFormat"] = 5] = "incorrectFormat";
12
+ ParseError[ParseError["referenceIsIncorrectType"] = 6] = "referenceIsIncorrectType";
13
+ ParseError[ParseError["valueAndDefault"] = 7] = "valueAndDefault";
14
+ ParseError[ParseError["defaultNotSupported"] = 8] = "defaultNotSupported";
15
+ ParseError[ParseError["multipleOperationsNotAllowed"] = 9] = "multipleOperationsNotAllowed";
16
+ ParseError[ParseError["requiredFieldNotDefined"] = 10] = "requiredFieldNotDefined";
17
+ ParseError[ParseError["languageNotSupported"] = 11] = "languageNotSupported";
18
+ ParseError[ParseError["invalidJSON"] = 12] = "invalidJSON";
19
+ ParseError[ParseError["noLanguageFound"] = 13] = "noLanguageFound";
20
+ ParseError[ParseError["cannotDeleteNodeFromModify"] = 14] = "cannotDeleteNodeFromModify";
21
+ ParseError[ParseError["nestedModifyObjectNotAllowed"] = 15] = "nestedModifyObjectNotAllowed";
22
+ ParseError[ParseError["infinityNotSupported"] = 16] = "infinityNotSupported";
23
+ })(ParseError || (exports.ParseError = ParseError = {}));
24
+ //# sourceMappingURL=error.js.map
@@ -0,0 +1,5 @@
1
+ export * from './types';
2
+ export * from './validateSchema';
3
+ export * from './walker';
4
+ export * from './set';
5
+ export * from './display';
package/dist/index.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./types"), exports);
18
+ __exportStar(require("./validateSchema"), exports);
19
+ __exportStar(require("./walker"), exports);
20
+ __exportStar(require("./set"), exports);
21
+ __exportStar(require("./display"), exports);
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,187 @@
1
+ export declare const languages: {
2
+ ab: string;
3
+ aa: string;
4
+ af: string;
5
+ ak: string;
6
+ sq: string;
7
+ am: string;
8
+ ar: string;
9
+ an: string;
10
+ hy: string;
11
+ as: string;
12
+ av: string;
13
+ ae: string;
14
+ ay: string;
15
+ az: string;
16
+ bm: string;
17
+ ba: string;
18
+ eu: string;
19
+ be: string;
20
+ bn: string;
21
+ bh: string;
22
+ bi: string;
23
+ bs: string;
24
+ br: string;
25
+ bg: string;
26
+ my: string;
27
+ ca: string;
28
+ km: string;
29
+ ch: string;
30
+ ce: string;
31
+ ny: string;
32
+ zh: string;
33
+ cu: string;
34
+ cv: string;
35
+ kw: string;
36
+ co: string;
37
+ cr: string;
38
+ hr: string;
39
+ cs: string;
40
+ da: string;
41
+ dv: string;
42
+ nl: string;
43
+ dz: string;
44
+ en: string;
45
+ eo: string;
46
+ et: string;
47
+ ee: string;
48
+ fo: string;
49
+ fj: string;
50
+ fi: string;
51
+ fr: string;
52
+ ff: string;
53
+ gd: string;
54
+ gl: string;
55
+ lg: string;
56
+ ka: string;
57
+ de: string;
58
+ ki: string;
59
+ el: string;
60
+ kl: string;
61
+ gn: string;
62
+ gu: string;
63
+ ht: string;
64
+ ha: string;
65
+ he: string;
66
+ hz: string;
67
+ hi: string;
68
+ ho: string;
69
+ hu: string;
70
+ is: string;
71
+ io: string;
72
+ ig: string;
73
+ id: string;
74
+ ia: string;
75
+ ie: string;
76
+ iu: string;
77
+ ik: string;
78
+ ga: string;
79
+ it: string;
80
+ ja: string;
81
+ jv: string;
82
+ kn: string;
83
+ kr: string;
84
+ ks: string;
85
+ kk: string;
86
+ rw: string;
87
+ kv: string;
88
+ kg: string;
89
+ ko: string;
90
+ kj: string;
91
+ ku: string;
92
+ ky: string;
93
+ lo: string;
94
+ la: string;
95
+ lv: string;
96
+ lb: string;
97
+ li: string;
98
+ ln: string;
99
+ lt: string;
100
+ lu: string;
101
+ mk: string;
102
+ mg: string;
103
+ ms: string;
104
+ ml: string;
105
+ mt: string;
106
+ gv: string;
107
+ mi: string;
108
+ mr: string;
109
+ mh: string;
110
+ ro: string;
111
+ mn: string;
112
+ na: string;
113
+ nv: string;
114
+ nd: string;
115
+ ng: string;
116
+ ne: string;
117
+ se: string;
118
+ no: string;
119
+ nb: string;
120
+ nn: string;
121
+ ii: string;
122
+ oc: string;
123
+ oj: string;
124
+ or: string;
125
+ om: string;
126
+ os: string;
127
+ pi: string;
128
+ pa: string;
129
+ ps: string;
130
+ fa: string;
131
+ pl: string;
132
+ pt: string;
133
+ qu: string;
134
+ rm: string;
135
+ rn: string;
136
+ ru: string;
137
+ sm: string;
138
+ sg: string;
139
+ sa: string;
140
+ sc: string;
141
+ sr: string;
142
+ sn: string;
143
+ sd: string;
144
+ si: string;
145
+ sk: string;
146
+ sl: string;
147
+ so: string;
148
+ st: string;
149
+ nr: string;
150
+ es: string;
151
+ su: string;
152
+ sw: string;
153
+ ss: string;
154
+ sv: string;
155
+ tl: string;
156
+ ty: string;
157
+ tg: string;
158
+ ta: string;
159
+ tt: string;
160
+ te: string;
161
+ th: string;
162
+ bo: string;
163
+ ti: string;
164
+ to: string;
165
+ ts: string;
166
+ tn: string;
167
+ tr: string;
168
+ tk: string;
169
+ tw: string;
170
+ ug: string;
171
+ uk: string;
172
+ ur: string;
173
+ uz: string;
174
+ ve: string;
175
+ vi: string;
176
+ vo: string;
177
+ wa: string;
178
+ cy: string;
179
+ fy: string;
180
+ wo: string;
181
+ xh: string;
182
+ yi: string;
183
+ yo: string;
184
+ za: string;
185
+ zu: string;
186
+ };
187
+ export type Language = keyof typeof languages;