@dxos/compute 0.8.4-main.fcfe5033a5 → 0.8.4-staging.60fe92afc8
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 +102 -5
- package/README.md +36 -1
- package/dist/lib/neutral/StorageService.mjs +22 -0
- package/dist/lib/neutral/StorageService.mjs.map +7 -0
- package/dist/lib/neutral/chunk-2B2KSJT6.mjs +17 -0
- package/dist/lib/neutral/chunk-2B2KSJT6.mjs.map +7 -0
- package/dist/lib/neutral/chunk-AFBNVFAY.mjs +25 -0
- package/dist/lib/neutral/chunk-AFBNVFAY.mjs.map +7 -0
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/neutral/chunk-JAU4E4EK.mjs +53 -0
- package/dist/lib/neutral/chunk-JAU4E4EK.mjs.map +7 -0
- package/dist/lib/neutral/chunk-MEPE4HHX.mjs +21 -0
- package/dist/lib/neutral/chunk-MEPE4HHX.mjs.map +7 -0
- package/dist/lib/neutral/chunk-QZS4G6PE.mjs +240 -0
- package/dist/lib/neutral/chunk-QZS4G6PE.mjs.map +7 -0
- package/dist/lib/neutral/chunk-XEVFBI6S.mjs +62 -0
- package/dist/lib/neutral/chunk-XEVFBI6S.mjs.map +7 -0
- package/dist/lib/neutral/chunk-Z3Y7FQGN.mjs +174 -0
- package/dist/lib/neutral/chunk-Z3Y7FQGN.mjs.map +7 -0
- package/dist/lib/neutral/fib-HBDUGQW7.mjs +10 -0
- package/dist/lib/neutral/fib-HBDUGQW7.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +1179 -51
- package/dist/lib/neutral/index.mjs.map +4 -4
- package/dist/lib/neutral/meta.json +1 -1
- package/dist/lib/neutral/reply-RJIAJ6FV.mjs +10 -0
- package/dist/lib/neutral/reply-RJIAJ6FV.mjs.map +7 -0
- package/dist/lib/neutral/sleep-3T2CZ7RE.mjs +10 -0
- package/dist/lib/neutral/sleep-3T2CZ7RE.mjs.map +7 -0
- package/dist/lib/neutral/testing/index.mjs +27 -214
- package/dist/lib/neutral/testing/index.mjs.map +4 -4
- package/dist/types/src/Blueprint.d.ts +182 -0
- package/dist/types/src/Blueprint.d.ts.map +1 -0
- package/dist/types/src/Credential.d.ts +27 -0
- package/dist/types/src/Credential.d.ts.map +1 -0
- package/dist/types/src/Header.d.ts +9 -0
- package/dist/types/src/Header.d.ts.map +1 -0
- package/dist/types/src/Header.test.d.ts +2 -0
- package/dist/types/src/Header.test.d.ts.map +1 -0
- package/dist/types/src/LayerSpec.d.ts +56 -0
- package/dist/types/src/LayerSpec.d.ts.map +1 -0
- package/dist/types/src/McpServer.d.ts +26 -0
- package/dist/types/src/McpServer.d.ts.map +1 -0
- package/dist/types/src/Operation.d.ts +385 -0
- package/dist/types/src/Operation.d.ts.map +1 -0
- package/dist/types/src/OperationHandlerSet.d.ts +69 -0
- package/dist/types/src/OperationHandlerSet.d.ts.map +1 -0
- package/dist/types/src/OperationHandlerSet.test.d.ts +2 -0
- package/dist/types/src/OperationHandlerSet.test.d.ts.map +1 -0
- package/dist/types/src/OperationRegistry.d.ts +23 -0
- package/dist/types/src/OperationRegistry.d.ts.map +1 -0
- package/dist/types/src/Process.d.ts +255 -0
- package/dist/types/src/Process.d.ts.map +1 -0
- package/dist/types/src/Routine.d.ts +54 -0
- package/dist/types/src/Routine.d.ts.map +1 -0
- package/dist/types/src/Script.d.ts +21 -0
- package/dist/types/src/Script.d.ts.map +1 -0
- package/dist/types/src/ServiceResolver.d.ts +92 -0
- package/dist/types/src/ServiceResolver.d.ts.map +1 -0
- package/dist/types/src/StorageService.d.ts +58 -0
- package/dist/types/src/StorageService.d.ts.map +1 -0
- package/dist/types/src/Template.d.ts +61 -0
- package/dist/types/src/Template.d.ts.map +1 -0
- package/dist/types/src/Template.test.d.ts +2 -0
- package/dist/types/src/Template.test.d.ts.map +1 -0
- package/dist/types/src/Trace.d.ts +266 -0
- package/dist/types/src/Trace.d.ts.map +1 -0
- package/dist/types/src/Trigger.d.ts +192 -0
- package/dist/types/src/Trigger.d.ts.map +1 -0
- package/dist/types/src/TriggerEvent.d.ts +83 -0
- package/dist/types/src/TriggerEvent.d.ts.map +1 -0
- package/dist/types/src/errors.d.ts +308 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +18 -5
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/testing/definitions.d.ts +11 -0
- package/dist/types/src/testing/definitions.d.ts.map +1 -0
- package/dist/types/src/testing/fib.d.ts +8 -0
- package/dist/types/src/testing/fib.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +6 -4
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/reply.d.ts +4 -0
- package/dist/types/src/testing/reply.d.ts.map +1 -0
- package/dist/types/src/testing/sleep.d.ts +6 -0
- package/dist/types/src/testing/sleep.d.ts.map +1 -0
- package/dist/types/src/umbrella.test.d.ts +2 -0
- package/dist/types/src/umbrella.test.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +25 -36
- package/src/Blueprint.ts +224 -0
- package/src/Credential.ts +53 -0
- package/src/Header.test.ts +132 -0
- package/src/Header.ts +38 -0
- package/src/LayerSpec.ts +91 -0
- package/src/McpServer.ts +44 -0
- package/src/Operation.ts +646 -0
- package/src/OperationHandlerSet.test.ts +97 -0
- package/src/OperationHandlerSet.ts +181 -0
- package/src/OperationRegistry.ts +50 -0
- package/src/Process.ts +541 -0
- package/src/Routine.ts +68 -0
- package/src/Script.ts +33 -0
- package/src/ServiceResolver.ts +200 -0
- package/src/StorageService.ts +101 -0
- package/src/Template.test.ts +164 -0
- package/src/Template.ts +125 -0
- package/src/Trace.ts +363 -0
- package/src/Trigger.ts +196 -0
- package/src/TriggerEvent.ts +64 -0
- package/src/errors.ts +71 -0
- package/src/index.ts +20 -6
- package/src/testing/definitions.ts +51 -0
- package/src/testing/fib.ts +22 -0
- package/src/testing/index.ts +13 -5
- package/src/testing/reply.ts +18 -0
- package/src/testing/sleep.ts +16 -0
- package/src/umbrella.test.ts +51 -0
- package/dist/lib/neutral/chunk-JRV42MW3.mjs +0 -3101
- package/dist/lib/neutral/chunk-JRV42MW3.mjs.map +0 -7
- package/dist/types/src/compute-graph-registry.d.ts +0 -37
- package/dist/types/src/compute-graph-registry.d.ts.map +0 -1
- package/dist/types/src/compute-graph-registry.test.d.ts +0 -2
- package/dist/types/src/compute-graph-registry.test.d.ts.map +0 -1
- package/dist/types/src/compute-graph.d.ts +0 -67
- package/dist/types/src/compute-graph.d.ts.map +0 -1
- package/dist/types/src/compute-graph.test.d.ts +0 -2
- package/dist/types/src/compute-graph.test.d.ts.map +0 -1
- package/dist/types/src/compute-node.d.ts +0 -24
- package/dist/types/src/compute-node.d.ts.map +0 -1
- package/dist/types/src/functions/async-function.d.ts +0 -68
- package/dist/types/src/functions/async-function.d.ts.map +0 -1
- package/dist/types/src/functions/edge-function.d.ts +0 -20
- package/dist/types/src/functions/edge-function.d.ts.map +0 -1
- package/dist/types/src/functions/function-defs.d.ts +0 -11
- package/dist/types/src/functions/function-defs.d.ts.map +0 -1
- package/dist/types/src/functions/index.d.ts +0 -4
- package/dist/types/src/functions/index.d.ts.map +0 -1
- package/dist/types/src/hyperformula.test.d.ts +0 -2
- package/dist/types/src/hyperformula.test.d.ts.map +0 -1
- package/dist/types/src/testing/test-builder.d.ts +0 -15
- package/dist/types/src/testing/test-builder.d.ts.map +0 -1
- package/dist/types/src/testing/test-plugin.d.ts +0 -35
- package/dist/types/src/testing/test-plugin.d.ts.map +0 -1
- package/dist/types/src/testing/test-runtime.d.ts +0 -10
- package/dist/types/src/testing/test-runtime.d.ts.map +0 -1
- package/dist/types/src/testing/util.d.ts +0 -2
- package/dist/types/src/testing/util.d.ts.map +0 -1
- package/dist/types/src/types/hyperformula.d.ts +0 -8
- package/dist/types/src/types/hyperformula.d.ts.map +0 -1
- package/dist/types/src/types/index.d.ts +0 -3
- package/dist/types/src/types/index.d.ts.map +0 -1
- package/dist/types/src/types/types.d.ts +0 -20
- package/dist/types/src/types/types.d.ts.map +0 -1
- package/dist/types/src/types/types.test.d.ts +0 -2
- package/dist/types/src/types/types.test.d.ts.map +0 -1
- package/dist/types/src/util.d.ts +0 -2
- package/dist/types/src/util.d.ts.map +0 -1
- package/src/compute-graph-registry.test.ts +0 -167
- package/src/compute-graph-registry.ts +0 -97
- package/src/compute-graph.test.ts +0 -139
- package/src/compute-graph.ts +0 -265
- package/src/compute-node.ts +0 -59
- package/src/functions/async-function.ts +0 -184
- package/src/functions/edge-function.ts +0 -108
- package/src/functions/function-defs.ts +0 -2427
- package/src/functions/index.ts +0 -7
- package/src/hyperformula.test.ts +0 -14
- package/src/testing/test-builder.ts +0 -59
- package/src/testing/test-plugin.ts +0 -98
- package/src/testing/test-runtime.ts +0 -53
- package/src/testing/util.ts +0 -9
- package/src/types/hyperformula.ts +0 -19
- package/src/types/index.ts +0 -6
- package/src/types/types.test.ts +0 -91
- package/src/types/types.ts +0 -80
- package/src/util.ts +0 -8
package/src/functions/index.ts
DELETED
package/src/hyperformula.test.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { describe, expect, test } from 'vitest';
|
|
6
|
-
|
|
7
|
-
import { HyperFormula } from '@dxos/vendor-hyperformula';
|
|
8
|
-
|
|
9
|
-
describe('hyperformula', () => {
|
|
10
|
-
test('sanity test', async () => {
|
|
11
|
-
const hf = HyperFormula.buildEmpty({ licenseKey: 'gpl-v3' });
|
|
12
|
-
expect(hf).to.exist;
|
|
13
|
-
});
|
|
14
|
-
});
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Client, type ClientOptions } from '@dxos/client';
|
|
6
|
-
import { type Context, Resource } from '@dxos/context';
|
|
7
|
-
import { invariant } from '@dxos/invariant';
|
|
8
|
-
|
|
9
|
-
import { type ComputeGraphOptions, ComputeGraphRegistry } from '../compute-graph-registry';
|
|
10
|
-
import { createMockedComputeRuntimeProvider } from './test-runtime';
|
|
11
|
-
|
|
12
|
-
export type TestBuilderOptions = ClientOptions & Partial<ComputeGraphOptions>;
|
|
13
|
-
|
|
14
|
-
// TODO(burdon): Reconcile with @dxos/client/testing.
|
|
15
|
-
export class TestBuilder extends Resource {
|
|
16
|
-
private _client?: Client;
|
|
17
|
-
private _registry?: ComputeGraphRegistry;
|
|
18
|
-
|
|
19
|
-
constructor(private readonly _options: TestBuilderOptions = {}) {
|
|
20
|
-
super();
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
get ctx(): Context {
|
|
24
|
-
return this._ctx;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
get client(): Client {
|
|
28
|
-
invariant(this._client);
|
|
29
|
-
return this._client;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
get registry(): ComputeGraphRegistry {
|
|
33
|
-
invariant(this._registry);
|
|
34
|
-
return this._registry;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
override async _open(): Promise<void> {
|
|
38
|
-
const client = new Client(this._options);
|
|
39
|
-
await client.initialize();
|
|
40
|
-
await client.halo.createIdentity();
|
|
41
|
-
this._client = client;
|
|
42
|
-
this._ctx.onDispose(async () => {
|
|
43
|
-
await client.destroy();
|
|
44
|
-
this._client = undefined;
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
const registry = new ComputeGraphRegistry({
|
|
48
|
-
...this._options,
|
|
49
|
-
// Provide a default runtime provider for tests if not supplied.
|
|
50
|
-
computeRuntime: this._options.computeRuntime ?? createMockedComputeRuntimeProvider(),
|
|
51
|
-
});
|
|
52
|
-
await registry.open();
|
|
53
|
-
this._registry = registry;
|
|
54
|
-
this._ctx.onDispose(async () => {
|
|
55
|
-
await registry.close();
|
|
56
|
-
this._registry = undefined;
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
}
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { getDeep } from '@dxos/util';
|
|
6
|
-
import { type InterpreterState, type ProcedureAst } from '@dxos/vendor-hyperformula';
|
|
7
|
-
import { FunctionArgumentType } from '@dxos/vendor-hyperformula';
|
|
8
|
-
|
|
9
|
-
import { type ComputeGraphPlugin } from '../compute-graph-registry';
|
|
10
|
-
import { type AsyncFunction, AsyncFunctionPlugin } from '../functions';
|
|
11
|
-
import { parseNumberString } from '../util';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Testing functions run locally (not run via EDGE).
|
|
15
|
-
* https://hyperformula.handsontable.com/guide/custom-functions.html#add-a-simple-custom-function
|
|
16
|
-
*/
|
|
17
|
-
export class TestPlugin extends AsyncFunctionPlugin {
|
|
18
|
-
/**
|
|
19
|
-
* Simple local function returns input value.
|
|
20
|
-
*/
|
|
21
|
-
test(ast: ProcedureAst, state: InterpreterState) {
|
|
22
|
-
const handler: AsyncFunction = async (_value) => {
|
|
23
|
-
return _value;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
return this.runAsyncFunction(ast, state, handler);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Simple local function returns random number.
|
|
31
|
-
*/
|
|
32
|
-
random(ast: ProcedureAst, state: InterpreterState) {
|
|
33
|
-
const handler: AsyncFunction = async () => {
|
|
34
|
-
return Math.random();
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
return this.runAsyncFunction(ast, state, handler);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Async HTTP function.
|
|
42
|
-
*/
|
|
43
|
-
crypto(ast: ProcedureAst, state: InterpreterState) {
|
|
44
|
-
const handler: AsyncFunction = async (_currency) => {
|
|
45
|
-
const currency = (_currency || 'USD').toUpperCase();
|
|
46
|
-
const result = await fetch(`https://api.coindesk.com/v1/bpi/currentprice/${currency}.json`);
|
|
47
|
-
const data = await result.json();
|
|
48
|
-
const rate = getDeep<string>(data, ['bpi', currency, 'rate']);
|
|
49
|
-
if (!rate) {
|
|
50
|
-
return NaN;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return parseNumberString(rate);
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
return this.runAsyncFunction(ast, state, handler, { ttl: 10_000 });
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
TestPlugin.implementedFunctions = {
|
|
61
|
-
TEST: {
|
|
62
|
-
method: 'test',
|
|
63
|
-
parameters: [{ argumentType: FunctionArgumentType.NUMBER, optionalArg: false }],
|
|
64
|
-
isVolatile: true,
|
|
65
|
-
},
|
|
66
|
-
|
|
67
|
-
RANDOM: {
|
|
68
|
-
method: 'random',
|
|
69
|
-
parameters: [],
|
|
70
|
-
isVolatile: true,
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
CRYPTO: {
|
|
74
|
-
method: 'crypto',
|
|
75
|
-
parameters: [{ argumentType: FunctionArgumentType.STRING, optionalArg: true }],
|
|
76
|
-
isVolatile: true,
|
|
77
|
-
},
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
export const TestPluginTranslations = {
|
|
81
|
-
enGB: {
|
|
82
|
-
TEST: 'Returns input value',
|
|
83
|
-
RANDOM: 'Random number',
|
|
84
|
-
CRYPTO: 'Crypto token value',
|
|
85
|
-
},
|
|
86
|
-
enUS: {
|
|
87
|
-
TEST: 'Returns input value',
|
|
88
|
-
RANDOM: 'Random number',
|
|
89
|
-
CRYPTO: 'Crypto token value',
|
|
90
|
-
},
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
export const testFunctionPlugins: ComputeGraphPlugin[] = [
|
|
94
|
-
{
|
|
95
|
-
plugin: TestPlugin,
|
|
96
|
-
translations: TestPluginTranslations,
|
|
97
|
-
},
|
|
98
|
-
];
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import * as Effect from 'effect/Effect';
|
|
6
|
-
import * as Layer from 'effect/Layer';
|
|
7
|
-
import * as ManagedRuntime from 'effect/ManagedRuntime';
|
|
8
|
-
|
|
9
|
-
import { type SpaceId } from '@dxos/client/echo';
|
|
10
|
-
import { Operation, OperationHandlerSet } from '@dxos/operation';
|
|
11
|
-
|
|
12
|
-
import { type FunctionsRuntimeProvider } from '../compute-graph-registry';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Minimal compute runtime provider for tests and Storybook.
|
|
16
|
-
* Will cause errors if used for actual runtime.
|
|
17
|
-
*/
|
|
18
|
-
export const createMockedComputeRuntimeProvider = ({
|
|
19
|
-
functions,
|
|
20
|
-
}: { functions?: OperationHandlerSet.OperationHandlerSet } = {}): FunctionsRuntimeProvider => {
|
|
21
|
-
return {
|
|
22
|
-
getRuntime: (_spaceId: SpaceId) =>
|
|
23
|
-
ManagedRuntime.make(
|
|
24
|
-
functions
|
|
25
|
-
? Layer.effect(
|
|
26
|
-
Operation.Service,
|
|
27
|
-
Effect.gen(function* () {
|
|
28
|
-
const handlers = yield* functions.handlers;
|
|
29
|
-
return {
|
|
30
|
-
invoke: (op: any, ...args: any[]) => {
|
|
31
|
-
const handler = handlers.find((h: any) => h.meta.key === op.meta.key);
|
|
32
|
-
if (!handler) {
|
|
33
|
-
return Effect.die(`No handler found for operation: ${op.meta.key}`);
|
|
34
|
-
}
|
|
35
|
-
const result = handler.handler(args[0]);
|
|
36
|
-
if (Effect.isEffect(result)) {
|
|
37
|
-
return result as Effect.Effect<unknown>;
|
|
38
|
-
}
|
|
39
|
-
return Effect.promise(() => Promise.resolve(result));
|
|
40
|
-
},
|
|
41
|
-
schedule: () => Effect.void,
|
|
42
|
-
invokePromise: async () => ({ error: new Error('Not implemented in test runtime') }),
|
|
43
|
-
} as Operation.OperationService;
|
|
44
|
-
}),
|
|
45
|
-
)
|
|
46
|
-
: Layer.succeed(Operation.Service, {
|
|
47
|
-
invoke: () => Effect.die('Operation.Service not available in test.'),
|
|
48
|
-
schedule: () => Effect.die('Operation.Service not available in test.'),
|
|
49
|
-
invokePromise: async () => ({ error: new Error('Not available') }),
|
|
50
|
-
} as any),
|
|
51
|
-
),
|
|
52
|
-
};
|
|
53
|
-
};
|
package/src/testing/util.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
type SimpleCellAddress,
|
|
7
|
-
type SimpleCellRange,
|
|
8
|
-
type SimpleDate,
|
|
9
|
-
type SimpleDateTime,
|
|
10
|
-
} from '@dxos/vendor-hyperformula';
|
|
11
|
-
import { DetailedCellError, ExportedCellChange } from '@dxos/vendor-hyperformula';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Re-exported from vendored hyperformula package.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
export { DetailedCellError, ExportedCellChange };
|
|
18
|
-
|
|
19
|
-
export type { SimpleCellRange, SimpleCellAddress, SimpleDate, SimpleDateTime };
|
package/src/types/index.ts
DELETED
package/src/types/types.test.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { getIndices, getIndicesAbove, getIndicesBelow, getIndicesBetween, sortByIndex } from '@tldraw/indices';
|
|
6
|
-
import { describe, expect, test } from 'vitest';
|
|
7
|
-
|
|
8
|
-
import { addressFromA1Notation, addressToA1Notation, inRange, rangeFromA1Notation, rangeToA1Notation } from './types';
|
|
9
|
-
|
|
10
|
-
describe('cell', () => {
|
|
11
|
-
test('posToA1Notation', () => {
|
|
12
|
-
expect(addressToA1Notation({ col: 0, row: 0 })).to.eq('A1');
|
|
13
|
-
expect(addressFromA1Notation('C2')).to.deep.eq({ col: 2, row: 1 });
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test('rangeToA1Notation', () => {
|
|
17
|
-
expect(rangeToA1Notation({ from: addressFromA1Notation('A1'), to: addressFromA1Notation('A5') })).to.eq('A1:A5');
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test('inRange', () => {
|
|
21
|
-
const range = rangeFromA1Notation('A1:C5');
|
|
22
|
-
expect(inRange(range, addressFromA1Notation('A1'))).to.be.true;
|
|
23
|
-
expect(inRange(range, addressFromA1Notation('C5'))).to.be.true;
|
|
24
|
-
expect(inRange(range, addressFromA1Notation('A6'))).to.be.false;
|
|
25
|
-
expect(inRange(range, addressFromA1Notation('D5'))).to.be.false;
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
// TODO(burdon): Move to model.test.ts
|
|
29
|
-
test('index', () => {
|
|
30
|
-
// Pre-allocated grid.
|
|
31
|
-
const n = 5;
|
|
32
|
-
const columns = getIndices(n - 1);
|
|
33
|
-
const rows = getIndices(n - 1);
|
|
34
|
-
|
|
35
|
-
const pickOne = (indices: string[]) => indices[Math.floor(Math.random() * indices.length)];
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Insert an index into the allocated list.
|
|
39
|
-
* Randomly picks from n values between indexes to support probabilistic concurrency.
|
|
40
|
-
*/
|
|
41
|
-
const insertIndex = (indices: string[], i: number, n = 20) => {
|
|
42
|
-
if (i === 0) {
|
|
43
|
-
const idx = pickOne(getIndicesBelow(indices[0], n));
|
|
44
|
-
indices.splice(0, 0, idx);
|
|
45
|
-
} else if (i >= indices.length) {
|
|
46
|
-
// Reallocate if > current bounds.
|
|
47
|
-
// TODO(burdon): Is this OK if this happens concurrently?
|
|
48
|
-
indices.splice(indices.length, 0, ...getIndicesAbove(indices[indices.length - 1], i + 1 - indices.length));
|
|
49
|
-
} else {
|
|
50
|
-
const idx = pickOne(getIndicesBetween(indices[i - 1], indices[i], n));
|
|
51
|
-
indices.splice(i, 0, idx);
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
// Values.
|
|
56
|
-
const cells: Record<string, any> = {};
|
|
57
|
-
const setCell = (cell: string, value: any) => {
|
|
58
|
-
const { col, row } = addressFromA1Notation(cell);
|
|
59
|
-
// Reallocate if > current bounds.
|
|
60
|
-
if (col >= columns.length) {
|
|
61
|
-
insertIndex(columns, col);
|
|
62
|
-
}
|
|
63
|
-
if (row >= rows.length) {
|
|
64
|
-
insertIndex(rows, row);
|
|
65
|
-
}
|
|
66
|
-
const index = `${columns[col]}@${rows[row]}`;
|
|
67
|
-
cells[index] = value;
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
expect(addressFromA1Notation('A1')).to.deep.eq({ col: 0, row: 0 });
|
|
71
|
-
|
|
72
|
-
expect(columns).to.deep.eq(['a1', 'a2', 'a3', 'a4', 'a5']);
|
|
73
|
-
insertIndex(columns, 7);
|
|
74
|
-
expect(columns).to.deep.eq(['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8']);
|
|
75
|
-
|
|
76
|
-
setCell('A1', 100);
|
|
77
|
-
setCell('B1', 101);
|
|
78
|
-
|
|
79
|
-
insertIndex(columns, 1);
|
|
80
|
-
setCell('B1', 102);
|
|
81
|
-
|
|
82
|
-
setCell('J10', 104);
|
|
83
|
-
expect(columns).to.have.length(10);
|
|
84
|
-
expect(rows).to.have.length(10);
|
|
85
|
-
|
|
86
|
-
const entries = Object.entries(cells).map(([key, value]) => ({ index: key.split('@')[0], value }));
|
|
87
|
-
const sorted = entries.sort(sortByIndex);
|
|
88
|
-
const values = sorted.map(({ value }) => value);
|
|
89
|
-
expect(values).to.deep.eq([100, 102, 101, 104]);
|
|
90
|
-
});
|
|
91
|
-
});
|
package/src/types/types.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { invariant } from '@dxos/invariant';
|
|
6
|
-
|
|
7
|
-
// TODO(burdon): Reconcile with DxGridPlanePosition.
|
|
8
|
-
export type CellAddress = Record<'row' | 'col', number>;
|
|
9
|
-
// TODO(burdon): Unify and use Partial<CellRange> as needed.
|
|
10
|
-
export type CellRange = { from: CellAddress; to?: CellAddress };
|
|
11
|
-
export type CompleteCellRange = { from: CellAddress; to: CellAddress };
|
|
12
|
-
|
|
13
|
-
export type CellScalarValue = number | string | boolean | null;
|
|
14
|
-
|
|
15
|
-
export const RANGE_NOTATION = /^[A-Z]+[0-9]+(:[A-Z]+[0-9]+)?$/;
|
|
16
|
-
|
|
17
|
-
export const isFormula = (value: any): value is string => typeof value === 'string' && value.charAt(0) === '=';
|
|
18
|
-
|
|
19
|
-
export const cellEquals = (a: CellAddress | undefined, b: CellAddress | undefined) => {
|
|
20
|
-
return a?.col === b?.col && a?.row === b?.row;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export const columnLetter = (col: number): string => {
|
|
24
|
-
invariant(col < 676, `Invalid column: ${col}`); // 26^2
|
|
25
|
-
return (
|
|
26
|
-
(col >= 26 ? String.fromCharCode('A'.charCodeAt(0) + Math.floor(col / 26) - 1) : '') +
|
|
27
|
-
String.fromCharCode('A'.charCodeAt(0) + (col % 26))
|
|
28
|
-
);
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export const addressToA1Notation = ({ col, row }: CellAddress): string => {
|
|
32
|
-
return `${columnLetter(col)}${row + 1}`;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
// TODO(burdon): See (HF) simpleCellAddressFromString.
|
|
36
|
-
export const addressFromA1Notation = (ref: string): CellAddress => {
|
|
37
|
-
const match = ref.match(/([A-Z]+)(\d+)/);
|
|
38
|
-
invariant(match, `Invalid notation: ${ref}`);
|
|
39
|
-
return {
|
|
40
|
-
row: parseInt(match[2], 10) - 1,
|
|
41
|
-
col: match[1].split('').reduce((acc, c) => acc * 26 + c.charCodeAt(0) - 'A'.charCodeAt(0) + 1, 0) - 1,
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export const rangeToA1Notation = (range: CellRange) => {
|
|
46
|
-
return [range?.from && addressToA1Notation(range?.from), range?.to && addressToA1Notation(range?.to)]
|
|
47
|
-
.filter(Boolean)
|
|
48
|
-
.join(':');
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export const rangeFromA1Notation = (ref: string): CellRange => {
|
|
52
|
-
const [from, to] = ref.split(':').map(addressFromA1Notation);
|
|
53
|
-
return { from, to };
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
export const inRange = (range: CellRange | undefined, cell: CellAddress): boolean => {
|
|
57
|
-
if (!range) {
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const { from, to } = range;
|
|
62
|
-
if ((from && cellEquals(from, cell)) || (to && cellEquals(to, cell))) {
|
|
63
|
-
return true;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (!from || !to) {
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const { col: c1, row: r1 } = from;
|
|
71
|
-
const { col: c2, row: r2 } = to;
|
|
72
|
-
|
|
73
|
-
const cMin = Math.min(c1, c2);
|
|
74
|
-
const cMax = Math.max(c1, c2);
|
|
75
|
-
const rMin = Math.min(r1, r2);
|
|
76
|
-
const rMax = Math.max(r1, r2);
|
|
77
|
-
|
|
78
|
-
const { col, row } = cell;
|
|
79
|
-
return col >= cMin && col <= cMax && row >= rMin && row <= rMax;
|
|
80
|
-
};
|