@vuu-ui/vuu-utils 0.0.27

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/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@vuu-ui/vuu-utils",
3
+ "version": "0.0.27",
4
+ "author": "heswell",
5
+ "main": "src/index.ts",
6
+ "license": "Apache-2.0",
7
+ "files": [
8
+ "index.js",
9
+ "index.js.map",
10
+ "/src"
11
+ ],
12
+ "types": "src/index.ts",
13
+ "scripts": {
14
+ "build": "node ../../scripts/run-build.mjs",
15
+ "test": "vitest run",
16
+ "type-defs": "node ../../scripts/build-type-defs.mjs"
17
+ },
18
+ "devDependencies": {},
19
+ "peerDependencies": {
20
+ "react": "^17.0.2",
21
+ "react-dom": "^17.0.2"
22
+ }
23
+ }
@@ -0,0 +1,110 @@
1
+ import { WindowRange } from "./range-utils";
2
+ import { metadataKeys } from "./column-utils";
3
+
4
+ export type DataItem = string | number | boolean;
5
+ export type DataRow = [
6
+ /** index */
7
+ number,
8
+ /** render index */
9
+ number,
10
+ /** isLeaf */
11
+ boolean,
12
+ /** isExpanded */
13
+ boolean,
14
+ /** depth */
15
+ number,
16
+ /** child count */
17
+ number,
18
+ /** key */
19
+ string,
20
+ /** selected */
21
+ number,
22
+ /** data values */
23
+ ...DataItem[]
24
+ ];
25
+ export type RangeLike = { from: number; to: number };
26
+
27
+ const { KEY } = metadataKeys;
28
+
29
+ // const log = (message: string) =>
30
+ // console.log(`%c[DataWindow] ${message}`, "color: purple;font-weight: bold;");
31
+ export class DataWindow {
32
+ private range: WindowRange;
33
+ public data: DataRow[];
34
+ public rowCount = 0;
35
+ constructor({ from, to }: RangeLike) {
36
+ this.range = new WindowRange(from, to);
37
+ //internal data is always 0 based, we add range.from to determine an offset
38
+ this.data = new Array(to - from);
39
+ // window.dataWindow = this.data;
40
+ // log(`constructor initial range ${from} - ${to}`);
41
+ }
42
+
43
+ setRowCount = (rowCount: number) => {
44
+ // log(`rowCount => ${rowCount}`);
45
+ if (rowCount < this.data.length) {
46
+ this.data.length = rowCount;
47
+ }
48
+ this.rowCount = rowCount;
49
+ };
50
+
51
+ // return true if existing row was updated
52
+ add(data: DataRow) {
53
+ const [index] = data;
54
+ if (this.isWithinRange(index)) {
55
+ const internalIndex = index - this.range.from;
56
+ const isUpdate = this.data[internalIndex] !== undefined;
57
+ this.data[internalIndex] = data;
58
+ return isUpdate;
59
+ } else {
60
+ return false;
61
+ }
62
+ }
63
+
64
+ getAtIndex(index: number) {
65
+ return this.range.isWithin(index) &&
66
+ this.data[index - this.range.from] != null
67
+ ? this.data[index - this.range.from]
68
+ : undefined;
69
+ }
70
+
71
+ getByKey(key: string) {
72
+ return this.data.find((row) => row[KEY] === key);
73
+ }
74
+
75
+ isWithinRange(index: number) {
76
+ return this.range.isWithin(index) && index <= this.rowCount;
77
+ }
78
+
79
+ setRange(from: number, to: number) {
80
+ // log(`setRange ${from} ${to}`);
81
+ if (from !== this.range.from || to !== this.range.to) {
82
+ const [overlapFrom, overlapTo] = this.range.overlap(from, to);
83
+ const newData = new Array(to - from);
84
+ for (let i = overlapFrom; i < overlapTo; i++) {
85
+ const data = this.getAtIndex(i);
86
+ if (data) {
87
+ const index = i - from;
88
+ newData[index] = data;
89
+ }
90
+ }
91
+ this.data = newData;
92
+ this.range.from = from;
93
+ this.range.to = to;
94
+ }
95
+ }
96
+
97
+ hasData(from: number, to: number) {
98
+ const offset = this.range.from;
99
+ const start = from - offset;
100
+ const end = Math.min(to - offset - 1, this.rowCount - 1);
101
+ return this.data[start] !== undefined && this.data[end] !== undefined;
102
+ }
103
+
104
+ getData(from: number, to: number): any[] {
105
+ const { from: clientFrom, to: clientTo } = this.range;
106
+ const startOffset = Math.max(0, from - clientFrom);
107
+ const endOffset = Math.min(to - clientFrom, this.rowCount ?? to);
108
+ return this.data.slice(startOffset, endOffset);
109
+ }
110
+ }
@@ -0,0 +1,22 @@
1
+ export function arrayOfIndices(length: number): number[] {
2
+ // not the neatest, but far and away the fastest way to do this ...
3
+ const result = Array(length);
4
+ for (let i = 0; i < length; i++) {
5
+ result[i] = i;
6
+ }
7
+ return result;
8
+ }
9
+
10
+ export type PartitionTest<T> = (value: T, index: number) => boolean;
11
+
12
+ export function partition<T>(
13
+ array: T[],
14
+ test: PartitionTest<T>,
15
+ pass: T[] = [],
16
+ fail: T[] = []
17
+ ): [T[], T[]] {
18
+ for (let i = 0, len = array.length; i < len; i++) {
19
+ (test(array[i], i) ? pass : fail).push(array[i]);
20
+ }
21
+ return [pass, fail];
22
+ }
@@ -0,0 +1,133 @@
1
+ import {
2
+ ColumnDescriptor,
3
+ KeyedColumnDescriptor,
4
+ } from "@vuu-ui/vuu-datagrid/src/grid-model/gridModelTypes";
5
+ import { Row } from "./row-utils";
6
+
7
+ export interface ColumnMap {
8
+ [columnName: string]: number;
9
+ }
10
+
11
+ const SORT_ASC = "asc";
12
+
13
+ export type SortCriteriaItem = string | [string, "asc"]; // TODO where is 'desc'?
14
+
15
+ export function mapSortCriteria(
16
+ sortCriteria: SortCriteriaItem[],
17
+ columnMap: ColumnMap,
18
+ metadataOffset = 0
19
+ ): [number, "asc"][] {
20
+ return sortCriteria.map((s) => {
21
+ if (typeof s === "string") {
22
+ return [columnMap[s] + metadataOffset, "asc"];
23
+ } else if (Array.isArray(s)) {
24
+ const [columnName, sortDir] = s;
25
+ return [columnMap[columnName] + metadataOffset, sortDir || SORT_ASC];
26
+ } else {
27
+ throw Error("columnUtils.mapSortCriteria invalid input");
28
+ }
29
+ });
30
+ }
31
+
32
+ export function isKeyedColumn(
33
+ column: ColumnDescriptor
34
+ ): column is KeyedColumnDescriptor {
35
+ return typeof (column as KeyedColumnDescriptor).key === "number";
36
+ }
37
+
38
+ export const toColumnDescriptor = (name: string): ColumnDescriptor => ({
39
+ name,
40
+ });
41
+
42
+ export const toKeyedColumn = (
43
+ column: string | ColumnDescriptor,
44
+ key: number
45
+ ): KeyedColumnDescriptor => {
46
+ if (typeof column === "string") {
47
+ return { key, name: column };
48
+ }
49
+ if (isKeyedColumn(column)) {
50
+ return column;
51
+ }
52
+ return { ...column, key };
53
+ };
54
+
55
+ const EMPTY_COLUMN_MAP = {} as const;
56
+
57
+ export function buildColumnMap(columns?: (Column | string)[]): ColumnMap {
58
+ const start = metadataKeys.count;
59
+ if (columns) {
60
+ return columns.reduce((map, column, i) => {
61
+ if (typeof column === "string") {
62
+ map[column] = start + i;
63
+ } else if (typeof column.key === "number") {
64
+ map[column.name] = column.key;
65
+ } else {
66
+ map[column.name] = start + i;
67
+ }
68
+ return map;
69
+ }, {} as ColumnMap);
70
+ } else {
71
+ return EMPTY_COLUMN_MAP;
72
+ }
73
+ }
74
+
75
+ export function projectUpdates(updates: number[]): number[] {
76
+ const results: number[] = [];
77
+ const metadataOffset = metadataKeys.count - 2;
78
+ for (let i = 0; i < updates.length; i += 3) {
79
+ results[i] = updates[i] + metadataOffset;
80
+ results[i + 1] = updates[i + 1];
81
+ results[i + 2] = updates[i + 2];
82
+ }
83
+ return results;
84
+ }
85
+
86
+ export function projectColumns(
87
+ tableRowColumnMap: ColumnMap,
88
+ columns: ColumnDescriptor[]
89
+ ) {
90
+ const columnCount = columns.length;
91
+ const { IDX, RENDER_IDX, DEPTH, COUNT, KEY, SELECTED, count } = metadataKeys;
92
+ return (startIdx: number, offset: number, selectedRows: Row[] = []) =>
93
+ (row: Row, i: number) => {
94
+ // selectedRows are indices of rows within underlying dataset (not sorted or filtered)
95
+ // row is the original row from this set, with original index in IDX pos, which might
96
+ // be overwritten with a different value below if rows are sorted/filtered
97
+ const baseRowIdx: any = row[IDX]; // TODO
98
+ const out = [];
99
+ for (let i = 0; i < columnCount; i++) {
100
+ const colIdx = tableRowColumnMap[columns[i].name];
101
+ out[count + i] = row[colIdx];
102
+ }
103
+
104
+ out[IDX] = startIdx + i + offset;
105
+ out[RENDER_IDX] = 0;
106
+ out[DEPTH] = 0;
107
+ out[COUNT] = 0;
108
+ out[KEY] = row[tableRowColumnMap.KEY];
109
+ out[SELECTED] = selectedRows.includes(baseRowIdx) ? 1 : 0;
110
+ return out;
111
+ };
112
+ }
113
+
114
+ export type Meta = {
115
+ [key: string]: any;
116
+ } & any[];
117
+
118
+ export const metadataKeys = {
119
+ IDX: 0,
120
+ RENDER_IDX: 1,
121
+ IS_LEAF: 2,
122
+ IS_EXPANDED: 3,
123
+ DEPTH: 4,
124
+ COUNT: 5,
125
+ KEY: 6,
126
+ SELECTED: 7,
127
+ count: 8,
128
+ // TODO following only used in datamodel
129
+ PARENT_IDX: "parent_idx",
130
+ IDX_POINTER: "idx_pointer",
131
+ FILTER_COUNT: "filter_count",
132
+ NEXT_FILTER_IDX: "next_filter_idx",
133
+ } as const;
@@ -0,0 +1,8 @@
1
+ export const DataTypes = {
2
+ ROW_DATA: 'rowData',
3
+ FILTER_DATA: 'filterData',
4
+ FILTER_BINS: 'filterBins'
5
+ };
6
+
7
+ export const ASC = 'asc';
8
+ export const DSC = 'dsc';
@@ -0,0 +1,4 @@
1
+ //TODO
2
+ export const formatDate = (date: Date, format: string) => {
3
+ return date.toUTCString();
4
+ };
@@ -0,0 +1,139 @@
1
+ export interface Event {}
2
+
3
+ export type EventListener = (evtName: string, ...args: any[]) => void;
4
+
5
+ export type EventListenerMap = {
6
+ [eventName: string]: EventListener[] | EventListener;
7
+ };
8
+
9
+ function isArrayOfListeners(
10
+ listeners: EventListener | EventListener[]
11
+ ): listeners is EventListener[] {
12
+ return Array.isArray(listeners);
13
+ }
14
+
15
+ function isOnlyListener(
16
+ listeners: EventListener | EventListener[]
17
+ ): listeners is EventListener {
18
+ return !Array.isArray(listeners);
19
+ }
20
+
21
+ export interface IEventEmitter {
22
+ emit: (type: string, ...args: unknown[]) => void;
23
+ }
24
+
25
+ export class EventEmitter implements IEventEmitter {
26
+ private _events?: EventListenerMap;
27
+
28
+ constructor() {
29
+ this._events = {};
30
+ }
31
+
32
+ addListener(type: string, listener: EventListener) {
33
+ if (!this._events) {
34
+ this._events = {};
35
+ }
36
+
37
+ const listeners = this._events[type];
38
+
39
+ if (!listeners) {
40
+ this._events[type] = listener;
41
+ } else if (isArrayOfListeners(listeners)) {
42
+ listeners.push(listener);
43
+ } else if (isOnlyListener(listeners)) {
44
+ this._events[type] = [listeners, listener];
45
+ }
46
+ }
47
+
48
+ removeListener(type: string, listener: EventListener) {
49
+ if (!this._events || !this._events[type]) {
50
+ return;
51
+ }
52
+
53
+ const listenerOrListeners = this._events[type];
54
+ let position = -1;
55
+
56
+ if (listenerOrListeners === listener) {
57
+ delete this._events[type];
58
+ } else if (Array.isArray(listenerOrListeners)) {
59
+ for (let i = length; i-- > 0; ) {
60
+ if (listenerOrListeners[i] === listener) {
61
+ position = i;
62
+ break;
63
+ }
64
+ }
65
+
66
+ if (position < 0) {
67
+ return;
68
+ }
69
+
70
+ if (listenerOrListeners.length === 1) {
71
+ listenerOrListeners.length = 0;
72
+ delete this._events[type];
73
+ } else {
74
+ listenerOrListeners.splice(position, 1);
75
+ }
76
+ }
77
+ }
78
+
79
+ removeAllListeners(type: string) {
80
+ if (!this._events) {
81
+ return;
82
+ } else if (type === undefined) {
83
+ delete this._events;
84
+ } else {
85
+ delete this._events[type];
86
+ }
87
+ }
88
+
89
+ emit(type: string, ...args: unknown[]) {
90
+ if (this._events) {
91
+ const handler = this._events[type];
92
+ if (handler) {
93
+ invokeHandler(handler, type, args);
94
+ }
95
+ const wildcardHandler = this._events["*"];
96
+ if (wildcardHandler) {
97
+ invokeHandler(wildcardHandler, type, args);
98
+ }
99
+ }
100
+ }
101
+
102
+ once(type: string, listener: EventListener) {
103
+ const handler = (evtName: string, message: unknown) => {
104
+ this.removeListener(evtName, handler);
105
+ listener(evtName, message);
106
+ };
107
+
108
+ this.on(type, handler);
109
+ }
110
+
111
+ on(type: string, listener: EventListener) {
112
+ return this.addListener(type, listener);
113
+ }
114
+ }
115
+
116
+ function invokeHandler(
117
+ handler: EventListener | EventListener[],
118
+ type: string,
119
+ args: unknown[]
120
+ ) {
121
+ if (isArrayOfListeners(handler)) {
122
+ handler.slice().forEach((listener) => invokeHandler(listener, type, args));
123
+ } else {
124
+ switch (args.length) {
125
+ case 0:
126
+ handler(type);
127
+ break;
128
+ case 1:
129
+ handler(type, args[0]);
130
+ break;
131
+ case 2:
132
+ handler(type, args[0], args[1]);
133
+ break;
134
+ // slower
135
+ default:
136
+ handler.call(null, type, ...args);
137
+ }
138
+ }
139
+ }
@@ -0,0 +1 @@
1
+ export const getUniqueId = () => `hw-${Math.round(Math.random() * 1e5)}`;
package/src/index.ts ADDED
@@ -0,0 +1,14 @@
1
+ export { EventEmitter } from "./event-emitter";
2
+ export * from "./column-utils";
3
+ export * from "./DataWindow";
4
+ export * from "./date-utils";
5
+ export * from "./getUniqueId";
6
+ export * from "./input-utils";
7
+ export * from "./invariant";
8
+ export * from "./nanoid";
9
+ export * from "./array-utils";
10
+ export * from "./constants";
11
+ export * from "./range-utils";
12
+ export * from "./row-utils";
13
+ export * from "./simple-store";
14
+ export * from "./text-utils";
@@ -0,0 +1,50 @@
1
+ const actionKeys = {
2
+ Enter: 'Enter',
3
+ Delete: 'Delete'
4
+ };
5
+
6
+ const navigationKeys = {
7
+ Home: 'Home',
8
+ End: 'End',
9
+ ArrowRight: 'ArrowRight',
10
+ ArrowLeft: 'ArrowLeft',
11
+ ArrowDown: 'ArrowDown',
12
+ ArrowUp: 'ArrowUp',
13
+ Tab: 'Tab'
14
+ };
15
+ const functionKeys = {
16
+ F1: 'F1',
17
+ F2: 'F2',
18
+ F3: 'F3',
19
+ F4: 'F4',
20
+ F5: 'F5',
21
+ F6: 'F6',
22
+ F7: 'F7',
23
+ F8: 'F8',
24
+ F9: 'F9',
25
+ F10: 'F10',
26
+ F11: 'F11',
27
+ F12: 'F12'
28
+ };
29
+
30
+ const specialKeys = {
31
+ ...actionKeys,
32
+ ...navigationKeys,
33
+ ...functionKeys
34
+ };
35
+ type specialKey = keyof typeof specialKeys;
36
+
37
+ const isSpecialKey = (key: string): key is specialKey => key in specialKeys;
38
+
39
+ export const isCharacterKey = (evt: KeyboardEvent) => {
40
+ if (isSpecialKey(evt.key)) {
41
+ return false;
42
+ }
43
+ if (typeof evt.which === 'number' && evt.which > 0) {
44
+ return !evt.ctrlKey && !evt.metaKey && !evt.altKey && evt.which !== 8;
45
+ }
46
+ };
47
+
48
+ export const isQuoteKey = (evt: KeyboardEvent) => {
49
+ return evt.key === '"' || evt.key === "'";
50
+ };
@@ -0,0 +1,10 @@
1
+ export function invariant(condition: boolean, message: string) {
2
+ if (!condition) {
3
+ const error = new Error(message);
4
+ error.name = 'Invariant Violation';
5
+ // TODO what is framesToPop?
6
+ // @ts-ignore
7
+ error.framesToPop = 1; // we don't care about invariant's own frame
8
+ throw error;
9
+ }
10
+ }
@@ -0,0 +1,30 @@
1
+ // This is the index.browser.js file from nanoid
2
+ // couldn't get original code to work as npm import without crypro warnings -
3
+ // seemed to be picking up node version, not browser version
4
+
5
+ export const uuid = (size: number = 21): string => {
6
+ let id: string = '';
7
+ let bytes = crypto.getRandomValues(new Uint8Array(size));
8
+
9
+ // A compact alternative for `for (var i = 0; i < step; i++)`.
10
+ while (size--) {
11
+ // It is incorrect to use bytes exceeding the alphabet size.
12
+ // The following mask reduces the random byte in the 0-255 value
13
+ // range to the 0-63 value range. Therefore, adding hacks, such
14
+ // as empty string fallback or magic numbers, is unneccessary because
15
+ // the bitmask trims bytes down to the alphabet size.
16
+ let byte = bytes[size] & 63;
17
+ if (byte < 36) {
18
+ // `0-9a-z`
19
+ id += byte.toString(36);
20
+ } else if (byte < 62) {
21
+ // `A-Z`
22
+ id += (byte - 26).toString(36).toUpperCase();
23
+ } else if (byte < 63) {
24
+ id += '_';
25
+ } else {
26
+ id += '-';
27
+ }
28
+ }
29
+ return id;
30
+ };
@@ -0,0 +1,72 @@
1
+ export interface VuuRange {
2
+ from: number;
3
+ to: number;
4
+ bufferSize?: number;
5
+ reset?: boolean;
6
+ }
7
+
8
+ interface FromToRange {
9
+ from: number;
10
+ to: number;
11
+ }
12
+
13
+ export function getFullRange(
14
+ { from, to }: VuuRange,
15
+ bufferSize: number = 0,
16
+ rowCount: number = Number.MAX_SAFE_INTEGER
17
+ ): FromToRange {
18
+ if (bufferSize === 0) {
19
+ return { from, to: Math.min(to, rowCount) };
20
+ } else if (from === 0) {
21
+ return { from, to: Math.min(to + bufferSize, rowCount) };
22
+ } else {
23
+ const rangeSize = to - from;
24
+ const buff = Math.round(bufferSize / 2);
25
+ const shortfallBefore = from - buff < 0;
26
+ const shortFallAfter = rowCount - (to + buff) < 0;
27
+
28
+ if (shortfallBefore && shortFallAfter) {
29
+ return { from: 0, to: rowCount };
30
+ } else if (shortfallBefore) {
31
+ return { from: 0, to: rangeSize + bufferSize };
32
+ } else if (shortFallAfter) {
33
+ return { from: Math.max(0, rowCount - (rangeSize + bufferSize)), to: rowCount };
34
+ } else {
35
+ return { from: from - buff, to: to + buff };
36
+ }
37
+ }
38
+ }
39
+
40
+ export function resetRange({ from, to, bufferSize = 0 }: VuuRange): VuuRange {
41
+ return {
42
+ from: 0,
43
+ to: to - from,
44
+ bufferSize,
45
+ reset: true
46
+ };
47
+ }
48
+
49
+ export class WindowRange {
50
+ public from: number;
51
+ public to: number;
52
+
53
+ constructor(from: number, to: number) {
54
+ this.from = from;
55
+ this.to = to;
56
+ }
57
+
58
+ public isWithin(index: number) {
59
+ return index >= this.from && index < this.to;
60
+ }
61
+
62
+ //find the overlap of this range and a new one
63
+ public overlap(from: number, to: number): [number, number] {
64
+ return from >= this.to || to < this.from
65
+ ? [0, 0]
66
+ : [Math.max(from, this.from), Math.min(to, this.to)];
67
+ }
68
+
69
+ public copy(): WindowRange {
70
+ return new WindowRange(this.from, this.to);
71
+ }
72
+ }
@@ -0,0 +1,44 @@
1
+ export type RowIndex = {
2
+ [field: string]: number;
3
+ }
4
+
5
+ // TODO
6
+ export type Row = {
7
+ [strKey: string]: any;
8
+ } & any[];
9
+
10
+ export function addRowsToIndex(rows: Row[], index: RowIndex, indexField: string) {
11
+ for (let idx = 0, len = rows.length; idx < len; idx++) {
12
+ index[rows[idx][indexField]] = idx;
13
+ }
14
+ return index;
15
+ }
16
+
17
+ export function indexRows(rows: Row[], indexField: string) {
18
+ return addRowsToIndex(rows, {}, indexField);
19
+ }
20
+
21
+ export function isEmptyRow(row: Row) {
22
+ return row[0] === undefined;
23
+ }
24
+
25
+ // TODO rename
26
+ export function update(rows: Row[], updates: any) { // TODO
27
+ const results = rows.slice();
28
+ const [[offsetIdx]] = rows;
29
+ for (let i = 0; i < updates.length; i++) {
30
+ const idx = updates[i][0] - offsetIdx;
31
+ // slow, refactor for performance
32
+ if (rows[idx]) {
33
+ const row = rows[idx].slice();
34
+ for (let j = 1; j < updates[i].length; j += 3) {
35
+ row[updates[i][j]] = updates[i][j + 2];
36
+ }
37
+ results[idx] = row;
38
+ } else {
39
+ console.log(`row not found in rows`);
40
+ }
41
+ }
42
+
43
+ return results;
44
+ }
@@ -0,0 +1,33 @@
1
+ // intended use is singleton storage atom within a hook, see useViewserver for examples
2
+ import { EventEmitter } from "./event-emitter";
3
+
4
+ type StoreStatus = "" | "ready" | "loaded" | "loading";
5
+
6
+ export class SimpleStore<T = unknown> extends EventEmitter {
7
+ private _value: T;
8
+ private _status: StoreStatus = "";
9
+
10
+ constructor(value: T, status: StoreStatus = "") {
11
+ super();
12
+ this._value = value;
13
+ this._status = status;
14
+ }
15
+
16
+ set value(value: T) {
17
+ this._value = value;
18
+ this._status = "ready";
19
+ this.emit("loaded", value);
20
+ }
21
+
22
+ get value(): T {
23
+ return this._value;
24
+ }
25
+
26
+ set status(status: StoreStatus) {
27
+ this._status = status;
28
+ }
29
+
30
+ get status() {
31
+ return this._status;
32
+ }
33
+ }
@@ -0,0 +1,9 @@
1
+ export const lastWord = (text: string): string => {
2
+ const trimmedText = text.trim();
3
+ const pos = trimmedText.lastIndexOf(' ');
4
+ if (pos === -1) {
5
+ return trimmedText;
6
+ } else {
7
+ return trimmedText.slice(pos + 1);
8
+ }
9
+ };