pacc 6.2.1 → 6.4.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 CHANGED
@@ -32,6 +32,8 @@ const result = getAttribute({ a: [0,{ b: 4 }]}, "a[1].b");
32
32
  * [Parameters](#parameters-1)
33
33
  * [attributeIterator](#attributeiterator)
34
34
  * [Parameters](#parameters-2)
35
+ * [parseBytes](#parsebytes)
36
+ * [Parameters](#parameters-3)
35
37
  * [AttributeDefinition](#attributedefinition)
36
38
  * [Properties](#properties)
37
39
  * [default\_attribute](#default_attribute)
@@ -82,33 +84,37 @@ const result = getAttribute({ a: [0,{ b: 4 }]}, "a[1].b");
82
84
  * [timeout\_attribute](#timeout_attribute)
83
85
  * [language\_attribute](#language_attribute)
84
86
  * [environmentValues](#environmentvalues)
85
- * [Parameters](#parameters-3)
86
- * [expand](#expand)
87
87
  * [Parameters](#parameters-4)
88
- * [filter](#filter)
88
+ * [expand](#expand)
89
89
  * [Parameters](#parameters-5)
90
- * [setAttributes](#setattributes)
90
+ * [filter](#filter)
91
91
  * [Parameters](#parameters-6)
92
- * [getAttributes](#getattributes)
92
+ * [setAttributes](#setattributes)
93
93
  * [Parameters](#parameters-7)
94
- * [getAttributesJSON](#getattributesjson)
94
+ * [getAttributes](#getattributes)
95
95
  * [Parameters](#parameters-8)
96
+ * [getAttributesJSON](#getattributesjson)
97
+ * [Parameters](#parameters-9)
96
98
  * [tokens](#tokens)
97
99
  * [tokens](#tokens-1)
98
- * [Parameters](#parameters-9)
99
- * [setAttribute](#setattribute)
100
100
  * [Parameters](#parameters-10)
101
- * [getAttribute](#getattribute)
101
+ * [setAttribute](#setattribute)
102
102
  * [Parameters](#parameters-11)
103
- * [getAttributeAndOperator](#getattributeandoperator)
103
+ * [getAttribute](#getattribute)
104
104
  * [Parameters](#parameters-12)
105
- * [parseTime](#parsetime)
105
+ * [getAttributeAndOperator](#getattributeandoperator)
106
106
  * [Parameters](#parameters-13)
107
+ * [parseDuration](#parseduration)
108
+ * [Parameters](#parameters-14)
109
+ * [formatDuration](#formatduration)
110
+ * [Parameters](#parameters-15)
111
+ * [formatDurationISO](#formatdurationiso)
112
+ * [Parameters](#parameters-16)
107
113
  * [lookup](#lookup)
108
114
  * [Token](#token)
109
115
  * [Properties](#properties-1)
110
116
  * [createToken](#createtoken)
111
- * [Parameters](#parameters-14)
117
+ * [Parameters](#parameters-17)
112
118
  * [PLUS](#plus)
113
119
  * [MINUS](#minus)
114
120
  * [STAR](#star)
@@ -174,6 +180,16 @@ Iterate over all attributes.
174
180
 
175
181
  Returns **Iterable<\[[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>, [object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)]>**&#x20;
176
182
 
183
+ ## parseBytes
184
+
185
+ Convert byte size formatted string into number of bytes.
186
+
187
+ ### Parameters
188
+
189
+ * `value` **([number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) | [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String))**&#x20;
190
+
191
+ Returns **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** number of total bytes
192
+
177
193
  ## AttributeDefinition
178
194
 
179
195
  Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)
@@ -187,6 +203,7 @@ Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Globa
187
203
  * `collection` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**&#x20;
188
204
  * `private` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** should the value be shown
189
205
  * `credential` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** any type of credential
206
+ * `persistent` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** should we be stored
190
207
  * `depends` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** name of an attribute we depend on
191
208
  * `description` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** human readable
192
209
  * `default` **any?** the default value
@@ -518,13 +535,31 @@ The name may be a property path like 'a.b.c <='.
518
535
 
519
536
  Returns **\[any, [Token](#token)]** value associated with the given property name
520
537
 
521
- ## parseTime
538
+ ## parseDuration
539
+
540
+ Convert duration formatted string into number of seconds.
522
541
 
523
542
  ### Parameters
524
543
 
525
544
  * `value` **([number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) | [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String))**&#x20;
526
545
 
527
- Returns **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**&#x20;
546
+ Returns **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** seconds
547
+
548
+ ## formatDuration
549
+
550
+ ### Parameters
551
+
552
+ * `seconds` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**&#x20;
553
+
554
+ Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** formatted duration
555
+
556
+ ## formatDurationISO
557
+
558
+ ### Parameters
559
+
560
+ * `seconds` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**&#x20;
561
+
562
+ Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** formatted duration
528
563
 
529
564
  ## lookup
530
565
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "6.2.1",
3
+ "version": "6.4.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
package/src/bytes.mjs ADDED
@@ -0,0 +1,45 @@
1
+ export function formatBytes(bytes, decimals = 2) {
2
+ if (bytes === 0) return "0 Bytes";
3
+ if (bytes === 1) return "1 Byte";
4
+
5
+ const k = 1024;
6
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
7
+
8
+ return (
9
+ parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + " " + byteSizes[i]
10
+ );
11
+ }
12
+
13
+ const byteSizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
14
+
15
+ const units = {
16
+ b: 1,
17
+ kb: 1024,
18
+ mb: 1024 * 1024,
19
+ gb: 1024 * 1024 * 1024
20
+ };
21
+
22
+ /**
23
+ * Convert byte size formatted string into number of bytes.
24
+ * @param {number|string} value
25
+ * @returns {number} number of total bytes
26
+ */
27
+ export function parseBytes(value) {
28
+ if (typeof value === "string") {
29
+ let bytes = 0;
30
+
31
+ for (const match of value.matchAll(/([\d\.\-]+)\s*(\w*)/gi)) {
32
+ const v = parseFloat(match[1]);
33
+
34
+ if (match[2]) {
35
+ bytes += v * units[match[2]];
36
+ } else {
37
+ bytes += v;
38
+ }
39
+ }
40
+
41
+ return bytes;
42
+ }
43
+
44
+ return value;
45
+ }
@@ -10,6 +10,7 @@ import { types } from "./types.mjs";
10
10
  * @property {boolean} collection
11
11
  * @property {boolean} [private] should the value be shown
12
12
  * @property {boolean} [credential] any type of credential
13
+ * @property {boolean} [persistent] should we be stored
13
14
  * @property {string} [depends] name of an attribute we depend on
14
15
  * @property {string} [description] human readable
15
16
  * @property {any} [default] the default value
@@ -31,6 +32,7 @@ export const default_attribute = {
31
32
  writable: false,
32
33
  mandatory: false,
33
34
  collection: false,
35
+ persistent: false,
34
36
  private: false,
35
37
  credential: false,
36
38
  isKey: false
@@ -271,6 +273,8 @@ export { integer_attribute_writable as count_attribute_writable };
271
273
  */
272
274
  export { integer_attribute as size_attribute };
273
275
 
276
+ export const bytes_size_attribute = { ...default_attribute, type: types.byte_size };
277
+
274
278
  /**
275
279
  * @type {AttributeDefinition}
276
280
  */
package/src/module.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./types.mjs";
2
2
  export * from "./time.mjs";
3
+ export * from "./bytes.mjs";
3
4
  export * from "./attributes.mjs";
4
5
  export * from "./tokens.mjs";
5
6
  export * from "./filter.mjs";
package/src/time.mjs CHANGED
@@ -70,3 +70,35 @@ export function formatDuration(seconds) {
70
70
 
71
71
  return out.join(" ");
72
72
  }
73
+
74
+ const durationsISO = [
75
+ [86400, "D"],
76
+ [3600, "H"],
77
+ [60, "M"],
78
+ [1, "S"]
79
+ ];
80
+
81
+ /**
82
+ *
83
+ * @param {number} seconds
84
+ * @returns {string} formatted duration
85
+ */
86
+ export function formatDurationISO(seconds) {
87
+ let out = "P";
88
+ let t = false;
89
+
90
+ for (const d of durationsISO) {
91
+ if (seconds < 86400 && !t) {
92
+ out += "T";
93
+ t = true;
94
+ }
95
+
96
+ const n = Math.floor(seconds / Number(d[0]));
97
+ if (n > 0) {
98
+ out += `${n}${d[1]}`;
99
+ seconds -= n * Number(d[0]);
100
+ }
101
+ }
102
+
103
+ return out;
104
+ }
package/src/types.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  import { attributeIterator } from "./attributes.mjs";
2
2
  import { parseDuration } from "./time.mjs";
3
+ import { parseBytes } from "./bytes.mjs";
3
4
 
4
5
  /**
5
6
  * @typedef {Object} Type
@@ -47,6 +48,11 @@ export const types = {
47
48
  primitive: true,
48
49
  prepareValue: value => parseDuration(value) * 1000
49
50
  },
51
+ byte_size: {
52
+ name: "byte_size",
53
+ primitive: true,
54
+ prepareValue: parseBytes
55
+ },
50
56
  url: {
51
57
  name: "url",
52
58
  prepareValue: emptyStringIsUndefined,
@@ -0,0 +1,7 @@
1
+ export function formatBytes(bytes: any, decimals?: number): string;
2
+ /**
3
+ * Convert byte size formatted string into number of bytes.
4
+ * @param {number|string} value
5
+ * @returns {number} number of total bytes
6
+ */
7
+ export function parseBytes(value: number | string): number;
@@ -8,6 +8,7 @@
8
8
  * @property {boolean} collection
9
9
  * @property {boolean} [private] should the value be shown
10
10
  * @property {boolean} [credential] any type of credential
11
+ * @property {boolean} [persistent] should we be stored
11
12
  * @property {string} [depends] name of an attribute we depend on
12
13
  * @property {string} [description] human readable
13
14
  * @property {any} [default] the default value
@@ -37,6 +38,7 @@ export namespace string_collection_attribute {
37
38
  let _private: boolean;
38
39
  export { _private as private };
39
40
  export let credential: boolean;
41
+ export let persistent: boolean;
40
42
  export let depends: string;
41
43
  export let description: string;
42
44
  let _default: any;
@@ -130,6 +132,16 @@ export const integer_attribute: AttributeDefinition;
130
132
  * @type {AttributeDefinition}
131
133
  */
132
134
  export const integer_attribute_writable: AttributeDefinition;
135
+ export namespace bytes_size_attribute {
136
+ let type_1: {
137
+ name: string;
138
+ primitive: boolean;
139
+ prepareValue: typeof import("./bytes.mjs").parseBytes;
140
+ };
141
+ export { type_1 as type };
142
+ let collection_1: boolean;
143
+ export { collection_1 as collection };
144
+ }
133
145
  /**
134
146
  * @type {AttributeDefinition}
135
147
  */
@@ -196,6 +208,10 @@ export type AttributeDefinition = {
196
208
  * any type of credential
197
209
  */
198
210
  credential?: boolean;
211
+ /**
212
+ * should we be stored
213
+ */
214
+ persistent?: boolean;
199
215
  /**
200
216
  * name of an attribute we depend on
201
217
  */
@@ -1,5 +1,6 @@
1
1
  export * from "./types.mjs";
2
2
  export * from "./time.mjs";
3
+ export * from "./bytes.mjs";
3
4
  export * from "./attributes.mjs";
4
5
  export * from "./tokens.mjs";
5
6
  export * from "./filter.mjs";
package/types/time.d.mts CHANGED
@@ -10,3 +10,9 @@ export function parseDuration(value: number | string): number;
10
10
  * @returns {string} formatted duration
11
11
  */
12
12
  export function formatDuration(seconds: number): string;
13
+ /**
14
+ *
15
+ * @param {number} seconds
16
+ * @returns {string} formatted duration
17
+ */
18
+ export function formatDurationISO(seconds: number): string;
package/types/types.d.mts CHANGED
@@ -37,6 +37,11 @@ export const types: {
37
37
  primitive: boolean;
38
38
  prepareValue: (value: any) => number;
39
39
  };
40
+ byte_size: {
41
+ name: string;
42
+ primitive: boolean;
43
+ prepareValue: typeof parseBytes;
44
+ };
40
45
  url: {
41
46
  name: string;
42
47
  prepareValue: (value: any) => any;
@@ -52,3 +57,4 @@ export type Type = {
52
57
  primitive?: boolean;
53
58
  prepareValue?: Function;
54
59
  };
60
+ import { parseBytes } from "./bytes.mjs";