@candidstartup/infinisheet-types 0.7.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/LICENSE +29 -0
- package/README.md +10 -0
- package/dist/index.d.ts +135 -0
- package/dist/index.js +149 -0
- package/dist/index.js.map +1 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-2024, Tim Wiegand
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[](https://www.npmjs.com/package/@candidstartup/infinisheet-types)
|
|
2
|
+
[](https://www.npmjs.com/package/@candidstartup/infinisheet-types)
|
|
3
|
+
[](https://www.npmjs.com/package/@candidstartup/infinisheet-types)
|
|
4
|
+
[](https://github.com/TheCandidStartup/infinisheet/actions/workflows/build.yml)
|
|
5
|
+
|
|
6
|
+
# @candidstartup/infinisheet-types
|
|
7
|
+
|
|
8
|
+
Common types shared by InfiniSheet frontend and backend packages.
|
|
9
|
+
|
|
10
|
+
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface used to determine the size and positioning offset for items in a single dimension.
|
|
3
|
+
*/
|
|
4
|
+
interface ItemOffsetMapping {
|
|
5
|
+
/** Size of item with given index */
|
|
6
|
+
itemSize(itemIndex: number): number;
|
|
7
|
+
/** Offset from start of container to specified item
|
|
8
|
+
*
|
|
9
|
+
* `itemOffset(n)` should be equal to `Sum{i:0->n-1}(itemSize(i))`
|
|
10
|
+
*
|
|
11
|
+
* To efficiently support large containers, cost should be `O(logn)` or better.
|
|
12
|
+
*/
|
|
13
|
+
itemOffset(itemIndex: number): number;
|
|
14
|
+
/** Given an offset, return the index of the item that intersects that offset, together with the start offset of that item */
|
|
15
|
+
offsetToItem(offset: number): [itemIndex: number, startOffset: number];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Implementation of {@link ItemOffsetMapping} for use when all items have a fixed size
|
|
20
|
+
*/
|
|
21
|
+
declare class FixedSizeItemOffsetMapping implements ItemOffsetMapping {
|
|
22
|
+
#private;
|
|
23
|
+
/**
|
|
24
|
+
* @param itemSize - Size to use for all items
|
|
25
|
+
*/
|
|
26
|
+
constructor(itemSize: number);
|
|
27
|
+
itemSize(_itemIndex: number): number;
|
|
28
|
+
itemOffset(itemIndex: number): number;
|
|
29
|
+
offsetToItem(offset: number): [itemIndex: number, startOffset: number];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Implementation of {@link ItemOffsetMapping} for use when initial items have variable sizes.
|
|
34
|
+
*/
|
|
35
|
+
declare class VariableSizeItemOffsetMapping implements ItemOffsetMapping {
|
|
36
|
+
#private;
|
|
37
|
+
/**
|
|
38
|
+
* @param defaultItemSize - Size to use for all other items
|
|
39
|
+
* @param sizes - Array of sizes to use for the initial items, one size per item
|
|
40
|
+
*/
|
|
41
|
+
constructor(defaultItemSize: number, sizes: number[]);
|
|
42
|
+
itemSize(itemIndex: number): number;
|
|
43
|
+
itemOffset(itemIndex: number): number;
|
|
44
|
+
offsetToItem(offset: number): [itemIndex: number, startOffset: number];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Possible spreadsheet error values
|
|
48
|
+
*
|
|
49
|
+
* Includes those that can be returned by `ERROR.TYPE` and additional values
|
|
50
|
+
* introduced by more recent versions of Excel.
|
|
51
|
+
*/
|
|
52
|
+
type CellErrorValue = '#NULL!' | '#DIV/0!' | '#VALUE!' | '#REF!' | '#NAME?' | '#NUM!' | '#N/A' | '#GETTING_DATA' | '#SPILL!' | '#UNKNOWN!' | '#FIELD!' | '#CALC!';
|
|
53
|
+
/** Type that represents an error value stored in a cell
|
|
54
|
+
*
|
|
55
|
+
* Defined as a discriminated union so that additional cell value types
|
|
56
|
+
* can be added in future.
|
|
57
|
+
*/
|
|
58
|
+
interface CellError {
|
|
59
|
+
/** Discriminated union tag */
|
|
60
|
+
type: 'CellError';
|
|
61
|
+
/** {@link CellErrorValue | Error Value} */
|
|
62
|
+
value: CellErrorValue;
|
|
63
|
+
}
|
|
64
|
+
/** Possible types for a cell value
|
|
65
|
+
*
|
|
66
|
+
* The native JavaScript types string, number and boolean represent the *Text*, *Number* and *Logical*
|
|
67
|
+
* spreadsheet data types. {@link CellError} represents an *Error Value*.
|
|
68
|
+
*
|
|
69
|
+
* Undefined is used to represent a cell with no defined value. Null represents a cell that has been
|
|
70
|
+
* explicitly marked as empty.
|
|
71
|
+
*/
|
|
72
|
+
type CellValue = string | number | boolean | null | undefined | CellError;
|
|
73
|
+
/**
|
|
74
|
+
* Interface used to access the data in a spreadsheet
|
|
75
|
+
*
|
|
76
|
+
* The data exposed through the interface may change over time outside of any changes made through the interface. The caller
|
|
77
|
+
* can use the {@link subscribe} method to be notified when the data changes. When reading data, the caller must first request
|
|
78
|
+
* a snapshot using {@link getSnapshot}. All values returned by gettors are relative to a specified snapshot. The values will be
|
|
79
|
+
* consistent with each other as long as the same snapshot is used.
|
|
80
|
+
*
|
|
81
|
+
* @typeParam Snapshot - Type of snapshot. Implementations are free to use whatever type makes sense for them.
|
|
82
|
+
*/
|
|
83
|
+
interface SpreadsheetData<Snapshot> {
|
|
84
|
+
/** Subscribe to data changes */
|
|
85
|
+
subscribe(onDataChange: () => void): () => void;
|
|
86
|
+
/** Return a snapshot to use when accessing values at a consistent point in time */
|
|
87
|
+
getSnapshot(): Snapshot;
|
|
88
|
+
/** Number of rows in the spreadsheet */
|
|
89
|
+
getRowCount(snapshot: Snapshot): number;
|
|
90
|
+
/** {@link ItemOffsetMapping} which describes sizes and offsets to start of rows */
|
|
91
|
+
getRowItemOffsetMapping(snapshot: Snapshot): ItemOffsetMapping;
|
|
92
|
+
/** Number of columns in the spreadsheet */
|
|
93
|
+
getColumnCount(snapshot: Snapshot): number;
|
|
94
|
+
/** {@link ItemOffsetMapping} which describes sizes and offsets to start of columns */
|
|
95
|
+
getColumnItemOffsetMapping(snapshot: Snapshot): ItemOffsetMapping;
|
|
96
|
+
/** Value of specified cell using 0-based row and column indexes */
|
|
97
|
+
getCellValue(snapshot: Snapshot, row: number, column: number): CellValue;
|
|
98
|
+
/** Format of specified cell using 0-based row and column indexes */
|
|
99
|
+
getCellFormat(snapshot: Snapshot, row: number, column: number): string | undefined;
|
|
100
|
+
/** Set value and format of specified cell
|
|
101
|
+
*
|
|
102
|
+
* @returns True if the change was successfully applied
|
|
103
|
+
*/
|
|
104
|
+
setCellValueAndFormat(row: number, column: number, value: CellValue, format: string | undefined): boolean;
|
|
105
|
+
}
|
|
106
|
+
declare class EmptySpreadsheetData implements SpreadsheetData<number> {
|
|
107
|
+
subscribe(_onDataChange: () => void): () => void;
|
|
108
|
+
getSnapshot(): number;
|
|
109
|
+
getRowCount(_snapshot: number): number;
|
|
110
|
+
getRowItemOffsetMapping(_snapshot: number): ItemOffsetMapping;
|
|
111
|
+
getColumnCount(_snapshot: number): number;
|
|
112
|
+
getColumnItemOffsetMapping(_snapshot: number): ItemOffsetMapping;
|
|
113
|
+
getCellValue(_snapshot: number, _row: number, _column: number): CellValue;
|
|
114
|
+
getCellFormat(_snapshot: number, _row: number, _column: number): string | undefined;
|
|
115
|
+
setCellValueAndFormat(_row: number, _column: number, _value: CellValue, _format: string | undefined): boolean;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Classic Spreadsheet reference to Column (e.g "A") */
|
|
119
|
+
type ColRef = string;
|
|
120
|
+
/** Classic Spreadsheet reference to Cell (e.g. "A1"), Row (e.g. "1") or Column (e.g "A") */
|
|
121
|
+
type RowColRef = string;
|
|
122
|
+
/** Equivalent to {@link RowColRef} as coordinate pair. "A1" has coords [0,0], "1" is [0,undefined] and "A" is [undefined,0] */
|
|
123
|
+
type RowColCoords = [row: number | undefined, col: number | undefined];
|
|
124
|
+
/** Converts a {@link ColRef} to the 0-based index of the column */
|
|
125
|
+
declare function colRefToIndex(col: ColRef): number;
|
|
126
|
+
/** Converts a 0-based column index to a {@link ColRef} */
|
|
127
|
+
declare function indexToColRef(index: number): ColRef;
|
|
128
|
+
/** Splits a RowColRef into a 0-based row index and a {@link ColRef} */
|
|
129
|
+
declare function splitRowColRef(ref: RowColRef): [row: number | undefined, col: ColRef | undefined];
|
|
130
|
+
/** Converts a {@link RowColRef} to {@link RowColCoords} */
|
|
131
|
+
declare function rowColRefToCoords(ref: RowColRef): RowColCoords;
|
|
132
|
+
/** Converts {@link RowColCoords} to a {@link RowColRef} */
|
|
133
|
+
declare function rowColCoordsToRef(row: number | undefined, col: number | undefined): RowColRef;
|
|
134
|
+
|
|
135
|
+
export { type CellError, type CellErrorValue, type CellValue, type ColRef, EmptySpreadsheetData, FixedSizeItemOffsetMapping, type ItemOffsetMapping, type RowColCoords, type RowColRef, type SpreadsheetData, VariableSizeItemOffsetMapping, colRefToIndex, indexToColRef, rowColCoordsToRef, rowColRefToCoords, splitRowColRef };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { __classPrivateFieldSet, __classPrivateFieldGet } from 'tslib';
|
|
2
|
+
|
|
3
|
+
var _FixedSizeItemOffsetMapping_fixedItemSize;
|
|
4
|
+
/**
|
|
5
|
+
* Implementation of {@link ItemOffsetMapping} for use when all items have a fixed size
|
|
6
|
+
*/
|
|
7
|
+
class FixedSizeItemOffsetMapping {
|
|
8
|
+
/**
|
|
9
|
+
* @param itemSize - Size to use for all items
|
|
10
|
+
*/
|
|
11
|
+
constructor(itemSize) {
|
|
12
|
+
_FixedSizeItemOffsetMapping_fixedItemSize.set(this, void 0);
|
|
13
|
+
__classPrivateFieldSet(this, _FixedSizeItemOffsetMapping_fixedItemSize, itemSize, "f");
|
|
14
|
+
}
|
|
15
|
+
itemSize(_itemIndex) {
|
|
16
|
+
return __classPrivateFieldGet(this, _FixedSizeItemOffsetMapping_fixedItemSize, "f");
|
|
17
|
+
}
|
|
18
|
+
itemOffset(itemIndex) {
|
|
19
|
+
return itemIndex * __classPrivateFieldGet(this, _FixedSizeItemOffsetMapping_fixedItemSize, "f");
|
|
20
|
+
}
|
|
21
|
+
offsetToItem(offset) {
|
|
22
|
+
const itemIndex = Math.floor(offset / __classPrivateFieldGet(this, _FixedSizeItemOffsetMapping_fixedItemSize, "f"));
|
|
23
|
+
const startOffset = itemIndex * __classPrivateFieldGet(this, _FixedSizeItemOffsetMapping_fixedItemSize, "f");
|
|
24
|
+
return [itemIndex, startOffset];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
_FixedSizeItemOffsetMapping_fixedItemSize = new WeakMap();
|
|
28
|
+
|
|
29
|
+
var _VariableSizeItemOffsetMapping_defaultItemSize, _VariableSizeItemOffsetMapping_sizes;
|
|
30
|
+
/**
|
|
31
|
+
* Implementation of {@link ItemOffsetMapping} for use when initial items have variable sizes.
|
|
32
|
+
*/
|
|
33
|
+
class VariableSizeItemOffsetMapping {
|
|
34
|
+
/**
|
|
35
|
+
* @param defaultItemSize - Size to use for all other items
|
|
36
|
+
* @param sizes - Array of sizes to use for the initial items, one size per item
|
|
37
|
+
*/
|
|
38
|
+
constructor(defaultItemSize, sizes) {
|
|
39
|
+
_VariableSizeItemOffsetMapping_defaultItemSize.set(this, void 0);
|
|
40
|
+
_VariableSizeItemOffsetMapping_sizes.set(this, void 0);
|
|
41
|
+
__classPrivateFieldSet(this, _VariableSizeItemOffsetMapping_defaultItemSize, defaultItemSize, "f");
|
|
42
|
+
__classPrivateFieldSet(this, _VariableSizeItemOffsetMapping_sizes, sizes, "f");
|
|
43
|
+
}
|
|
44
|
+
itemSize(itemIndex) {
|
|
45
|
+
return (itemIndex < __classPrivateFieldGet(this, _VariableSizeItemOffsetMapping_sizes, "f").length) ? __classPrivateFieldGet(this, _VariableSizeItemOffsetMapping_sizes, "f")[itemIndex] : __classPrivateFieldGet(this, _VariableSizeItemOffsetMapping_defaultItemSize, "f");
|
|
46
|
+
}
|
|
47
|
+
itemOffset(itemIndex) {
|
|
48
|
+
let offset = 0;
|
|
49
|
+
let length = __classPrivateFieldGet(this, _VariableSizeItemOffsetMapping_sizes, "f").length;
|
|
50
|
+
if (itemIndex > length) {
|
|
51
|
+
const numDefaultSize = itemIndex - length;
|
|
52
|
+
offset = numDefaultSize * __classPrivateFieldGet(this, _VariableSizeItemOffsetMapping_defaultItemSize, "f");
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
length = itemIndex;
|
|
56
|
+
}
|
|
57
|
+
for (let i = 0; i < length; i++) {
|
|
58
|
+
offset += __classPrivateFieldGet(this, _VariableSizeItemOffsetMapping_sizes, "f")[i];
|
|
59
|
+
}
|
|
60
|
+
return offset;
|
|
61
|
+
}
|
|
62
|
+
offsetToItem(offset) {
|
|
63
|
+
let startOffset = 0;
|
|
64
|
+
const length = __classPrivateFieldGet(this, _VariableSizeItemOffsetMapping_sizes, "f").length;
|
|
65
|
+
for (let i = 0; i < length; i++) {
|
|
66
|
+
const size = __classPrivateFieldGet(this, _VariableSizeItemOffsetMapping_sizes, "f")[i];
|
|
67
|
+
if (startOffset + size > offset) {
|
|
68
|
+
return [i, startOffset];
|
|
69
|
+
}
|
|
70
|
+
startOffset += size;
|
|
71
|
+
}
|
|
72
|
+
const itemIndex = Math.floor((offset - startOffset) / __classPrivateFieldGet(this, _VariableSizeItemOffsetMapping_defaultItemSize, "f"));
|
|
73
|
+
startOffset += itemIndex * __classPrivateFieldGet(this, _VariableSizeItemOffsetMapping_defaultItemSize, "f");
|
|
74
|
+
return [itemIndex + length, startOffset];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
_VariableSizeItemOffsetMapping_defaultItemSize = new WeakMap(), _VariableSizeItemOffsetMapping_sizes = new WeakMap();
|
|
78
|
+
|
|
79
|
+
const rowItemOffsetMapping = new FixedSizeItemOffsetMapping(30);
|
|
80
|
+
const columnItemOffsetMapping = new FixedSizeItemOffsetMapping(100);
|
|
81
|
+
class EmptySpreadsheetData {
|
|
82
|
+
subscribe(_onDataChange) { return () => { }; }
|
|
83
|
+
getSnapshot() { return 0; }
|
|
84
|
+
getRowCount(_snapshot) { return 0; }
|
|
85
|
+
getRowItemOffsetMapping(_snapshot) { return rowItemOffsetMapping; }
|
|
86
|
+
getColumnCount(_snapshot) { return 0; }
|
|
87
|
+
getColumnItemOffsetMapping(_snapshot) { return columnItemOffsetMapping; }
|
|
88
|
+
getCellValue(_snapshot, _row, _column) { return null; }
|
|
89
|
+
getCellFormat(_snapshot, _row, _column) { return undefined; }
|
|
90
|
+
setCellValueAndFormat(_row, _column, _value, _format) { return false; }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Converts a {@link ColRef} to the 0-based index of the column */
|
|
94
|
+
function colRefToIndex(col) {
|
|
95
|
+
let n = 0;
|
|
96
|
+
for (let i = 0; i < col.length; i++) {
|
|
97
|
+
n = col.charCodeAt(i) - 64 + n * 26;
|
|
98
|
+
}
|
|
99
|
+
return n - 1;
|
|
100
|
+
}
|
|
101
|
+
/** Converts a 0-based column index to a {@link ColRef} */
|
|
102
|
+
function indexToColRef(index) {
|
|
103
|
+
let ret = "";
|
|
104
|
+
index++;
|
|
105
|
+
while (index > 0) {
|
|
106
|
+
index--;
|
|
107
|
+
const remainder = index % 26;
|
|
108
|
+
index = Math.floor(index / 26);
|
|
109
|
+
ret = String.fromCharCode(65 + remainder) + ret;
|
|
110
|
+
}
|
|
111
|
+
return ret;
|
|
112
|
+
}
|
|
113
|
+
/** Splits a RowColRef into a 0-based row index and a {@link ColRef} */
|
|
114
|
+
function splitRowColRef(ref) {
|
|
115
|
+
const re = /^([A-Z]*)(\d*)$/;
|
|
116
|
+
const found = ref.match(re);
|
|
117
|
+
if (!found)
|
|
118
|
+
return [undefined, undefined];
|
|
119
|
+
const col = found[1];
|
|
120
|
+
const row = parseInt(found[2]);
|
|
121
|
+
return [(row > 0) ? row - 1 : undefined, col ? col : undefined];
|
|
122
|
+
}
|
|
123
|
+
/** Converts a {@link RowColRef} to {@link RowColCoords} */
|
|
124
|
+
function rowColRefToCoords(ref) {
|
|
125
|
+
const [row, col] = splitRowColRef(ref);
|
|
126
|
+
return [row, col ? colRefToIndex(col) : undefined];
|
|
127
|
+
}
|
|
128
|
+
/** Converts {@link RowColCoords} to a {@link RowColRef} */
|
|
129
|
+
function rowColCoordsToRef(row, col) {
|
|
130
|
+
if (row !== undefined) {
|
|
131
|
+
if (col !== undefined) {
|
|
132
|
+
return indexToColRef(col) + (row + 1);
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
return (row + 1).toString();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
if (col !== undefined) {
|
|
140
|
+
return indexToColRef(col);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
return "";
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export { EmptySpreadsheetData, FixedSizeItemOffsetMapping, VariableSizeItemOffsetMapping, colRefToIndex, indexToColRef, rowColCoordsToRef, rowColRefToCoords, splitRowColRef };
|
|
149
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/FixedSizeItemOffsetMapping.ts","../src/VariableSizeItemOffsetMapping.ts","../src/SpreadsheetData.ts","../src/RowColRef.ts"],"sourcesContent":["import type { ItemOffsetMapping } from './ItemOffsetMapping';\n\n/**\n * Implementation of {@link ItemOffsetMapping} for use when all items have a fixed size\n */\nexport class FixedSizeItemOffsetMapping implements ItemOffsetMapping {\n /**\n * @param itemSize - Size to use for all items\n */\n constructor (itemSize: number) {\n this.#fixedItemSize = itemSize;\n }\n\n itemSize(_itemIndex: number): number {\n return this.#fixedItemSize;\n }\n\n itemOffset(itemIndex: number): number {\n return itemIndex * this.#fixedItemSize;\n }\n\n offsetToItem(offset: number): [itemIndex: number, startOffset: number] {\n const itemIndex = Math.floor(offset / this.#fixedItemSize);\n const startOffset = itemIndex * this.#fixedItemSize;\n\n return [itemIndex, startOffset];\n }\n\n #fixedItemSize: number;\n}\n\n","import type { ItemOffsetMapping } from './ItemOffsetMapping';\n\n/**\n * Implementation of {@link ItemOffsetMapping} for use when initial items have variable sizes.\n */\nexport class VariableSizeItemOffsetMapping implements ItemOffsetMapping {\n /**\n * @param defaultItemSize - Size to use for all other items\n * @param sizes - Array of sizes to use for the initial items, one size per item\n */\n constructor (defaultItemSize: number, sizes: number[]) {\n this.#defaultItemSize = defaultItemSize;\n this.#sizes = sizes;\n }\n\n itemSize(itemIndex: number): number {\n return (itemIndex < this.#sizes.length) ? this.#sizes[itemIndex] : this.#defaultItemSize;\n }\n\n itemOffset(itemIndex: number): number {\n let offset = 0;\n let length = this.#sizes.length;\n if (itemIndex > length) {\n const numDefaultSize = itemIndex - length;\n offset = numDefaultSize * this.#defaultItemSize;\n } else {\n length = itemIndex;\n }\n \n for (let i = 0; i < length; i ++)\n {\n offset += this.#sizes[i];\n }\n\n return offset;\n }\n\n offsetToItem(offset: number): [itemIndex: number, startOffset: number] {\n let startOffset = 0;\n const length = this.#sizes.length;\n for (let i = 0; i < length; i ++) {\n const size = this.#sizes[i];\n if (startOffset + size > offset) {\n return [i, startOffset];\n }\n startOffset += size;\n }\n\n const itemIndex = Math.floor((offset - startOffset) / this.#defaultItemSize);\n startOffset += itemIndex * this.#defaultItemSize;\n\n return [itemIndex+length, startOffset];\n }\n\n #defaultItemSize: number;\n #sizes: number[];\n}\n","import type { ItemOffsetMapping } from \"./ItemOffsetMapping\";\nimport { FixedSizeItemOffsetMapping } from \"./FixedSizeItemOffsetMapping\";\n\n/** Possible spreadsheet error values\n * \n * Includes those that can be returned by `ERROR.TYPE` and additional values\n * introduced by more recent versions of Excel.\n*/\nexport type CellErrorValue = '#NULL!' | \n '#DIV/0!' |\n '#VALUE!' |\n '#REF!' |\n '#NAME?' |\n '#NUM!' |\n '#N/A' |\n '#GETTING_DATA' |\n '#SPILL!' |\n '#UNKNOWN!' |\n '#FIELD!' |\n '#CALC!';\n\n/** Type that represents an error value stored in a cell\n * \n * Defined as a discriminated union so that additional cell value types\n * can be added in future.\n */\nexport interface CellError {\n /** Discriminated union tag */\n type: 'CellError',\n\n /** {@link CellErrorValue | Error Value} */\n value: CellErrorValue;\n};\n\n/** Possible types for a cell value \n * \n * The native JavaScript types string, number and boolean represent the *Text*, *Number* and *Logical*\n * spreadsheet data types. {@link CellError} represents an *Error Value*.\n * \n * Undefined is used to represent a cell with no defined value. Null represents a cell that has been\n * explicitly marked as empty. \n*/\nexport type CellValue = string | number | boolean | null | undefined | CellError;\n\n/**\n * Interface used to access the data in a spreadsheet\n * \n * The data exposed through the interface may change over time outside of any changes made through the interface. The caller\n * can use the {@link subscribe} method to be notified when the data changes. When reading data, the caller must first request\n * a snapshot using {@link getSnapshot}. All values returned by gettors are relative to a specified snapshot. The values will be\n * consistent with each other as long as the same snapshot is used.\n * \n * @typeParam Snapshot - Type of snapshot. Implementations are free to use whatever type makes sense for them.\n */\nexport interface SpreadsheetData<Snapshot> {\n /** Subscribe to data changes */\n subscribe(onDataChange: () => void): () => void,\n\n /** Return a snapshot to use when accessing values at a consistent point in time */\n getSnapshot(): Snapshot,\n\n /** Number of rows in the spreadsheet */\n getRowCount(snapshot: Snapshot): number,\n\n /** {@link ItemOffsetMapping} which describes sizes and offsets to start of rows */\n getRowItemOffsetMapping(snapshot: Snapshot): ItemOffsetMapping,\n\n /** Number of columns in the spreadsheet */\n getColumnCount(snapshot: Snapshot): number,\n\n /** {@link ItemOffsetMapping} which describes sizes and offsets to start of columns */\n getColumnItemOffsetMapping(snapshot: Snapshot): ItemOffsetMapping,\n\n /** Value of specified cell using 0-based row and column indexes */\n getCellValue(snapshot: Snapshot, row: number, column: number): CellValue;\n\n /** Format of specified cell using 0-based row and column indexes */\n getCellFormat(snapshot: Snapshot, row: number, column: number): string | undefined;\n\n /** Set value and format of specified cell\n * \n * @returns True if the change was successfully applied\n */\n setCellValueAndFormat(row: number, column: number, value: CellValue, format: string | undefined): boolean;\n}\n\nconst rowItemOffsetMapping = new FixedSizeItemOffsetMapping(30);\nconst columnItemOffsetMapping = new FixedSizeItemOffsetMapping(100);\n\nexport class EmptySpreadsheetData implements SpreadsheetData<number> {\n subscribe(_onDataChange: () => void) { return () => {}; }\n getSnapshot() { return 0; }\n \n getRowCount(_snapshot: number) { return 0; }\n getRowItemOffsetMapping(_snapshot: number): ItemOffsetMapping { return rowItemOffsetMapping; }\n getColumnCount(_snapshot: number) { return 0; }\n getColumnItemOffsetMapping(_snapshot: number): ItemOffsetMapping { return columnItemOffsetMapping; }\n getCellValue(_snapshot: number, _row: number, _column: number): CellValue { return null; }\n getCellFormat(_snapshot: number, _row: number, _column: number): string|undefined { return undefined; }\n setCellValueAndFormat(_row: number, _column: number, _value: CellValue, _format: string | undefined): boolean { return false; }\n}\n\n","/** Classic Spreadsheet reference to Column (e.g \"A\") */\nexport type ColRef = string;\n\n/** Classic Spreadsheet reference to Cell (e.g. \"A1\"), Row (e.g. \"1\") or Column (e.g \"A\") */\nexport type RowColRef = string;\n\n/** Equivalent to {@link RowColRef} as coordinate pair. \"A1\" has coords [0,0], \"1\" is [0,undefined] and \"A\" is [undefined,0] */\nexport type RowColCoords = [row: number|undefined, col: number|undefined];\n\n/** Converts a {@link ColRef} to the 0-based index of the column */\nexport function colRefToIndex(col: ColRef): number {\n let n = 0;\n for (let i = 0; i < col.length; i ++) {\n n = col.charCodeAt(i) - 64 + n * 26;\n }\n return n-1;\n}\n\n/** Converts a 0-based column index to a {@link ColRef} */\nexport function indexToColRef(index: number): ColRef {\n let ret = \"\";\n index ++;\n while (index > 0) {\n index --;\n const remainder = index % 26;\n index = Math.floor(index / 26);\n ret = String.fromCharCode(65+remainder) + ret;\n }\n return ret;\n}\n\n/** Splits a RowColRef into a 0-based row index and a {@link ColRef} */\nexport function splitRowColRef(ref: RowColRef): [row: number|undefined, col: ColRef|undefined] {\n const re = /^([A-Z]*)(\\d*)$/;\n const found = ref.match(re);\n if (!found)\n return [undefined,undefined];\n\n const col = found[1];\n const row = parseInt(found[2]);\n return [(row>0) ? row-1 : undefined, col ? col : undefined];\n}\n\n/** Converts a {@link RowColRef} to {@link RowColCoords} */\nexport function rowColRefToCoords(ref: RowColRef): RowColCoords {\n const [row,col] = splitRowColRef(ref);\n return [row, col ? colRefToIndex(col) : undefined];\n}\n\n/** Converts {@link RowColCoords} to a {@link RowColRef} */\nexport function rowColCoordsToRef(row: number|undefined, col: number|undefined): RowColRef {\n if (row !== undefined) {\n if (col !== undefined) {\n return indexToColRef(col) + (row+1);\n } else {\n return (row+1).toString();\n }\n } else {\n if (col !== undefined) {\n return indexToColRef(col);\n } else {\n return \"\";\n }\n }\n}"],"names":[],"mappings":";;;AAEA;;AAEG;MACU,0BAA0B,CAAA;AACrC;;AAEG;AACH,IAAA,WAAA,CAAa,QAAgB,EAAA;QAmB7B,yCAAuB,CAAA,GAAA,CAAA,IAAA,EAAA,MAAA,CAAA;AAlBrB,QAAA,sBAAA,CAAA,IAAI,EAAA,yCAAA,EAAkB,QAAQ,EAAA,GAAA,CAAA;;AAGhC,IAAA,QAAQ,CAAC,UAAkB,EAAA;QACzB,OAAO,sBAAA,CAAA,IAAI,EAAA,yCAAA,EAAA,GAAA,CAAe;;AAG5B,IAAA,UAAU,CAAC,SAAiB,EAAA;AAC1B,QAAA,OAAO,SAAS,GAAG,sBAAA,CAAA,IAAI,iDAAe;;AAGxC,IAAA,YAAY,CAAC,MAAc,EAAA;AACzB,QAAA,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,sBAAA,CAAA,IAAI,EAAe,yCAAA,EAAA,GAAA,CAAA,CAAC;AAC1D,QAAA,MAAM,WAAW,GAAG,SAAS,GAAG,sBAAA,CAAA,IAAI,iDAAe;AAEnD,QAAA,OAAO,CAAC,SAAS,EAAE,WAAW,CAAC;;AAIlC;;;;AC3BD;;AAEG;MACU,6BAA6B,CAAA;AACxC;;;AAGG;IACH,WAAa,CAAA,eAAuB,EAAE,KAAe,EAAA;QA4Cr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CAAC;AAC7D;AAEA;AACM,SAAU,iBAAiB,CAAC,GAAc,EAAA;IAC9C,MAAM,CAAC,GAAG,EAAC,GAAG,CAAC,GAAG,cAAc,CAAC,GAAG,CAAC;AACrC,IAAA,OAAO,CAAC,GAAG,EAAE,GAAG,GAAG,aAAa,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;AACpD;AAEA;AACgB,SAAA,iBAAiB,CAAC,GAAqB,EAAE,GAAqB,EAAA;AAC5E,IAAA,IAAI,GAAG,KAAK,SAAS,EAAE;AACrB,QAAA,IAAI,GAAG,KAAK,SAAS,EAAE;YACrB,OAAO,aAAa,CAAC,GAAG,CAAC,IAAI,GAAG,GAAC,CAAC,CAAC;;aAC9B;YACL,OAAO,CAAC,GAAG,GAAC,CAAC,EAAE,QAAQ,EAAE;;;SAEtB;AACL,QAAA,IAAI,GAAG,KAAK,SAAS,EAAE;AACrB,YAAA,OAAO,aAAa,CAAC,GAAG,CAAC;;aACpB;AACL,YAAA,OAAO,EAAE;;;AAGf;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@candidstartup/infinisheet-types",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.7.0",
|
|
5
|
+
"description": "Common types for the InfiniSheet packages",
|
|
6
|
+
"author": "Tim Wiegand <tim.wiegand@thecandidstartup.org>",
|
|
7
|
+
"license": "BSD-3-Clause",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/TheCandidStartup/infinisheet.git",
|
|
11
|
+
"directory": "packages/infinisheet-types"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/TheCandidStartup/infinisheet/issues"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://github.com/TheCandidStartup/infinisheet/blob/main/packages/infinisheet-types/README.md",
|
|
17
|
+
"keywords": [
|
|
18
|
+
"typescript",
|
|
19
|
+
"spreadsheet",
|
|
20
|
+
"infinisheet"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"module": "./dist/index.js",
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"import": "./dist/index.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"dev": "vite",
|
|
39
|
+
"clean": "rimraf {dist,temp}",
|
|
40
|
+
"prebuild": "npm run clean",
|
|
41
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
42
|
+
"build-tsc": "tsc -p tsconfig.build.json",
|
|
43
|
+
"build-rollup": "rollup -c ../rollup.config.mjs",
|
|
44
|
+
"build": "npm run build-rollup",
|
|
45
|
+
"lint": "eslint . --report-unused-disable-directives --max-warnings 0",
|
|
46
|
+
"devapi": "api-extractor run --local",
|
|
47
|
+
"prodapi": "api-extractor run",
|
|
48
|
+
"devbuild": "npm run build && npm run lint && npm run devapi",
|
|
49
|
+
"test": "vitest"
|
|
50
|
+
},
|
|
51
|
+
"main": "index.js",
|
|
52
|
+
"gitHead": "5ea8ee7cef0e539dd15b844bd074a745d08b6ba6"
|
|
53
|
+
}
|