corebasic 1.0.218 → 1.0.219

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.
@@ -1,5 +1,6 @@
1
1
  import { entries, reduceRanges, extractBounds, getNormalizedBounds } from './index.js';
2
2
  import { formatDate, fillDates } from './date.js';
3
+ import * as Dip from '../../elabase.js';
3
4
  const EXPLICIT_SUFFIX_POLICY_TYPES = new Set(["string", "date", "number"]);
4
5
  export async function suffix(doc, policies, insertMode, arg) {
5
6
  let suffixes = [""];
@@ -26,17 +27,17 @@ export async function suffix(doc, policies, insertMode, arg) {
26
27
  if (policy_type === "date") {
27
28
  keys = keys.map(key => formatDate(key, format));
28
29
  if (ranges.length) {
29
- ranges = extractBounds(reduceRanges(ranges));
30
- const from = ranges.from ?? policy.min; // policy.min is always inclusive i.e $gte
31
- const to = ranges.to ?? policy.max; // policy.max is always inclusive i.e $lte
32
- const fromOp = ranges.from ? ranges.fromOp : "$gte"; // policy.min is $gte
33
- const toOp = ranges.to ? ranges.toOp : "$lte"; // policy.max is $lte
30
+ const bounds = extractBounds(reduceRanges(ranges));
31
+ const from = bounds.from ?? policy.min; // policy.min is always inclusive i.e $gte
32
+ const to = bounds.to ?? policy.max; // policy.max is always inclusive i.e $lte
33
+ const fromOp = bounds.from ? bounds.fromOp : "$gte"; // policy.min is $gte
34
+ const toOp = bounds.to ? bounds.toOp : "$lte"; // policy.max is $lte
34
35
  if (from === undefined || to === undefined) {
35
36
  keys = []; // Fall back to Dip.operation("ls").
36
37
  console.warn(`Warn: Missing suffix policy min/max bound for ${policy_type} range specified for key ${policy.key} in Dip`);
37
38
  }
38
39
  else {
39
- ranges = fillDates({ from, to, fromOp, toOp }, format);
40
+ let ranges = fillDates({ from, to, fromOp, toOp }, format);
40
41
  if (!ranges.length && policy.inferRange)
41
42
  ranges = [...new Set([from === undefined ? undefined : formatDate(from, format), to === undefined ? undefined : formatDate(to, format)].filter(item => item))];
42
43
  // console.log(ranges)
@@ -51,9 +52,9 @@ export async function suffix(doc, policies, insertMode, arg) {
51
52
  keys = [...new Set(keys)];
52
53
  }
53
54
  else if (policy_type === "number" && ranges.length) {
54
- ranges = getNormalizedBounds(extractBounds(reduceRanges(ranges)), "number");
55
- const from = ranges.from ?? policy.min; // policy.min is always inclusive i.e $gte
56
- const to = ranges.to ?? policy.max; // policy.max is always inclusive i.e $lte
55
+ const bounds = getNormalizedBounds(extractBounds(reduceRanges(ranges)), "number");
56
+ const from = bounds.from ?? policy.min; // policy.min is always inclusive i.e $gte
57
+ const to = bounds.to ?? policy.max; // policy.max is always inclusive i.e $lte
57
58
  if (from === undefined || to === undefined) {
58
59
  keys = []; // Fall back to Dip.operation("ls").
59
60
  console.warn(`Warn: Missing suffix policy min/max bound for ${policy_type} range specified for key ${policy.key} in Dip`);
@@ -1,3 +1,4 @@
1
+ // @ts-ignore
1
2
  import axios from 'axios';
2
3
  // import {curly} from 'node-libcurl'
3
4
  function hashStr(str) {
@@ -68,6 +69,6 @@ export default async function dipper(arg) {
68
69
  export const shard_stats = () => {
69
70
  let total = Object.values(shard_hits).reduce((partial, value) => partial + value, 0);
70
71
  let result = {};
71
- Object.entries(shard_hits).forEach(([key, value]) => result[key] = parseFloat(value * 100 / total).toFixed(2));
72
+ Object.entries(shard_hits).forEach(([key, value]) => result[key] = (value * 100 / total).toFixed(2));
72
73
  return result;
73
74
  };
@@ -6,7 +6,7 @@ import * as Cpp from './cpp.js';
6
6
  import { applySuffixPolicy } from './dip/suffix/policy.js';
7
7
  export const shard_stats = ShardStats;
8
8
  let Events = {
9
- send: function () { }
9
+ send: function (topic, data) { }
10
10
  };
11
11
  const url = process.env.DIP_URL || "http://127.0.0.1"; // Set DIP_URL to "http://dip" (Prod) or http://dip-dev (Dev) in CI/CD
12
12
  let port = process.env.DIP_PORT || 9401;
@@ -50,7 +50,7 @@ class DipMeta {
50
50
  }
51
51
  }
52
52
  export const isDipMeta = meta => meta instanceof DipMeta;
53
- export const globalMeta = args => {
53
+ export const globalMeta = (args) => {
54
54
  return new DipMeta({ ...args, company: "GLOBAL", outlet: "GLOBAL" });
55
55
  };
56
56
  export const companyMeta = (company, args) => {
@@ -1,4 +1,4 @@
1
- import {getNormalizedBounds} from './index.ts'
1
+ import {getNormalizedBounds, type Bounds} from './index.ts'
2
2
  import {addUTCYears, addUTCMonths, addUTCDays} from '../../utils.ts'
3
3
 
4
4
 
@@ -94,7 +94,7 @@ export function fillDates(bounds_t, format) {
94
94
  else
95
95
  step = "year";
96
96
 
97
- let bounds = { ...bounds_t, from: new Date(bounds_t.from), to: new Date(bounds_t.to) }
97
+ let bounds: Bounds = { ...bounds_t, from: new Date(bounds_t.from), to: new Date(bounds_t.to) }
98
98
 
99
99
  bounds = getNormalizedBounds(bounds, "date")
100
100
 
@@ -103,13 +103,13 @@ export function fillDates(bounds_t, format) {
103
103
 
104
104
  switch (step) {
105
105
  case "day":
106
- addUTCDays(bounds.from, 1)
106
+ addUTCDays(bounds.from as Date, 1)
107
107
  break;
108
108
  case "month":
109
- addUTCMonths(bounds.from, 1)
109
+ addUTCMonths(bounds.from as Date, 1)
110
110
  break;
111
111
  case "year":
112
- addUTCYears(bounds.from, 1)
112
+ addUTCYears(bounds.from as Date, 1)
113
113
  break;
114
114
  }
115
115
  }
@@ -160,11 +160,19 @@ export function reduceRanges(values) {
160
160
  return other;
161
161
  }
162
162
 
163
+ export type Bounds = {
164
+ values: any[],
165
+ from: number | string | Date;
166
+ to: number | string | Date;
167
+ fromOp: "$gt" | "$gte";
168
+ toOp: "$lt" | "$lte";
169
+ }
170
+
163
171
  // reduceRanges() resolves conflicting bounds before extractBounds() runs.
164
172
  // extractBounds() receives at most one lower bound and atmost one upper bound. reduceRanges() ensures this
165
173
  // The for loop in extractBounds() may look slightly misleading because it looks like it is designed to resolve conflicts: but reduceRanges() has already done the conflict resolution.
166
174
  // NOTE CRITICAL: extractBounds() MUST ONLY BE RUN ON THE OUTPUT OF reduceRanges()
167
- export function extractBounds(values) {
175
+ export function extractBounds(values): Bounds {
168
176
  let lower = null;
169
177
  let upper = null;
170
178
  let lowerOp = null
@@ -204,20 +212,20 @@ export function extractBounds(values) {
204
212
  }
205
213
 
206
214
  // Converts exclusive bounds ($gt/$lt) into inclusive bounds
207
- export function getNormalizedBounds(bounds_t, type) {
215
+ export function getNormalizedBounds(bounds_t: Bounds, type): Bounds {
208
216
  let bounds = {...bounds_t}
209
217
 
210
218
  if (type === "number") {
211
219
  if (bounds.fromOp === "$gt")
212
- bounds.from++;
220
+ (bounds.from as number)++;
213
221
  if (bounds.toOp === "$lt")
214
- bounds.to--;
222
+ (bounds.to as number)--;
215
223
  } else if (type === "date") {
216
224
  if (bounds.fromOp === "$gt")
217
- bounds.from = new Date(bounds.from.getTime() + 1);
225
+ bounds.from = new Date((bounds.from as Date).getTime() + 1);
218
226
 
219
227
  if (bounds.toOp === "$lt")
220
- bounds.to = new Date(bounds.to.getTime() - 1);
228
+ bounds.to = new Date((bounds.to as Date).getTime() - 1);
221
229
  }
222
230
 
223
231
  return bounds
@@ -1,5 +1,6 @@
1
- import {entries, reduceRanges, extractBounds, getNormalizedBounds} from './index.ts'
1
+ import {entries, reduceRanges, extractBounds, getNormalizedBounds, type Bounds} from './index.ts'
2
2
  import {formatDate, fillDates} from './date.ts'
3
+ import * as Dip from '../../elabase.ts'
3
4
 
4
5
  type Arg = { db?: string; collection?: string; [key: string]: any }
5
6
 
@@ -37,16 +38,16 @@ export async function suffix(doc, policies, insertMode?: boolean, arg?: Arg) {
37
38
  if (policy_type === "date") {
38
39
  keys = keys.map(key => formatDate(key, format))
39
40
  if (ranges.length) {
40
- ranges = extractBounds(reduceRanges(ranges))
41
- const from = ranges.from ?? policy.min // policy.min is always inclusive i.e $gte
42
- const to = ranges.to ?? policy.max // policy.max is always inclusive i.e $lte
43
- const fromOp = ranges.from ? ranges.fromOp : "$gte" // policy.min is $gte
44
- const toOp = ranges.to ? ranges.toOp : "$lte" // policy.max is $lte
41
+ const bounds = extractBounds(reduceRanges(ranges))
42
+ const from = bounds.from ?? policy.min // policy.min is always inclusive i.e $gte
43
+ const to = bounds.to ?? policy.max // policy.max is always inclusive i.e $lte
44
+ const fromOp = bounds.from ? bounds.fromOp : "$gte" // policy.min is $gte
45
+ const toOp = bounds.to ? bounds.toOp : "$lte" // policy.max is $lte
45
46
  if (from === undefined || to === undefined) {
46
47
  keys = [] // Fall back to Dip.operation("ls").
47
48
  console.warn(`Warn: Missing suffix policy min/max bound for ${policy_type} range specified for key ${policy.key} in Dip`)
48
49
  } else {
49
- ranges = fillDates({from, to, fromOp, toOp}, format)
50
+ let ranges = fillDates({from, to, fromOp, toOp}, format)
50
51
  if (!ranges.length && policy.inferRange)
51
52
  ranges = [...new Set([from === undefined ? undefined : formatDate(from, format), to === undefined ? undefined : formatDate(to, format)].filter(item => item))]
52
53
  // console.log(ranges)
@@ -59,9 +60,9 @@ export async function suffix(doc, policies, insertMode?: boolean, arg?: Arg) {
59
60
  }
60
61
  keys = [...new Set(keys)];
61
62
  } else if (policy_type === "number" && ranges.length) {
62
- ranges = getNormalizedBounds(extractBounds(reduceRanges(ranges)), "number")
63
- const from = ranges.from ?? policy.min // policy.min is always inclusive i.e $gte
64
- const to = ranges.to ?? policy.max // policy.max is always inclusive i.e $lte
63
+ const bounds = getNormalizedBounds(extractBounds(reduceRanges(ranges)), "number")
64
+ const from = bounds.from ?? policy.min // policy.min is always inclusive i.e $gte
65
+ const to = bounds.to ?? policy.max // policy.max is always inclusive i.e $lte
65
66
  if (from === undefined || to === undefined) {
66
67
  keys = [] // Fall back to Dip.operation("ls").
67
68
  console.warn(`Warn: Missing suffix policy min/max bound for ${policy_type} range specified for key ${policy.key} in Dip`)
package/libs/dipper.ts CHANGED
@@ -1,3 +1,4 @@
1
+ // @ts-ignore
1
2
  import axios from 'axios'
2
3
  // import {curly} from 'node-libcurl'
3
4
 
@@ -84,8 +85,8 @@ export default async function dipper(arg) {
84
85
 
85
86
 
86
87
  export const shard_stats = () => {
87
- let total = Object.values(shard_hits).reduce( (partial, value) => partial + value, 0)
88
+ let total: number = (Object.values(shard_hits) as number[]).reduce( (partial: number, value: number) => partial + value, 0)
88
89
  let result = {}
89
- Object.entries(shard_hits).forEach( ([key,value]) => result[key] = parseFloat(value * 100 / total).toFixed(2) )
90
+ Object.entries(shard_hits).forEach( ([key, value]) => result[key] = ((value as number) * 100 / total).toFixed(2) )
90
91
  return result
91
92
  }
package/libs/elabase.ts CHANGED
@@ -13,7 +13,7 @@ export const shard_stats = ShardStats
13
13
 
14
14
 
15
15
  let Events = {
16
- send: function() { }
16
+ send: function(topic?: string, data?: any) { }
17
17
  }
18
18
 
19
19
 
@@ -63,6 +63,9 @@ function deepFreeze(obj) {
63
63
  return obj;
64
64
  }
65
65
  class DipMeta {
66
+ declare company?: string;
67
+ declare outlet?: string;
68
+
66
69
  constructor(obj) {
67
70
  Object.assign(this, deepFreeze(obj));
68
71
  Object.freeze(this);
@@ -70,10 +73,10 @@ class DipMeta {
70
73
  }
71
74
  export const isDipMeta = meta => meta instanceof DipMeta
72
75
 
73
- export const globalMeta = args => {
76
+ export const globalMeta = (args?: any) => {
74
77
  return new DipMeta({ ...args, company: "GLOBAL", outlet: "GLOBAL" })
75
78
  }
76
- export const companyMeta = (company, args) => {
79
+ export const companyMeta = (company, args?: any) => {
77
80
  if (typeof company !== "string" && !Array.isArray(company))
78
81
  throw new Error("Error: Invalid company provided in Dip.companyMeta()")
79
82
  if (!company || (typeof company === "string" && company.trim() === "") || company === "GLOBAL" || (Array.isArray(company) && (!company.length || company.some(item => typeof item !== "string" || item.trim() === "" || item === "GLOBAL")) ))
@@ -81,7 +84,7 @@ export const companyMeta = (company, args) => {
81
84
 
82
85
  return new DipMeta({...args, outlet: "GLOBAL", company})
83
86
  }
84
- export const outletMeta = (outlet, args) => {
87
+ export const outletMeta = (outlet, args?: any) => {
85
88
  if (typeof outlet !== "string" && !Array.isArray(outlet))
86
89
  throw new Error("Error: Invalid outlet provided in Dip.outletMeta()")
87
90
  if (!outlet || (typeof outlet === "string" && outlet.trim() === "") || outlet === "GLOBAL" || (Array.isArray(outlet) && (!outlet.length || outlet.some(item => typeof item !== "string" || item.trim() === "" || item === "GLOBAL")) ))
@@ -90,7 +93,7 @@ export const outletMeta = (outlet, args) => {
90
93
  return new DipMeta({...args, company: "GLOBAL", outlet})
91
94
  }
92
95
 
93
- export const customMeta = (company, outlet, args) => {
96
+ export const customMeta = (company, outlet, args?: any) => {
94
97
 
95
98
  if (typeof company !== "string" && !Array.isArray(company))
96
99
  throw new Error("Error: Invalid company provided in Dip.customMeta()")
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.218",
4
+ "version": "1.0.219",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "./index.ts",