@orangecharts/wrun-chart-host 0.1.0 → 0.2.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.
@@ -0,0 +1,92 @@
1
+ /**
2
+ * The market a wrun table row or feed line is about (UW-PARITY H4): a `panel.table` frame's and a
3
+ * `draw.feed` frame's `markets`, one entry per row or line, each null or one market. Two forms, in
4
+ * symbol codes (wrun-symbol-code.util):
5
+ *
6
+ * { contract: [root_code, expiry_ms, side_cp, strike] } one US listed (OPRA) option contract
7
+ * { ticker: code } one stock or ETF
8
+ *
9
+ * The grammar restates the om kit's rule (wrun-host/row-markets.ts `wrunRowMarketIssue`) with its
10
+ * words, so a frame refuses alike on every host. The contract is the OPRA cells' own identity
11
+ * (option_prints, option_contract_flow and option_contract_state lead with it): the option root as
12
+ * listed (1 to 6 characters, the OSI root), the expiry in epoch milliseconds, side_cp +1 a call and
13
+ * -1 a put, the strike above 0 and below 100000.
14
+ *
15
+ * Pure and worker-safe: the frame grammar (wrun-frames.ts) parses with it. What a click on a named
16
+ * row opens is the host's (web: src/utility/wrun/wrun-row-market-target.util.ts and the row click).
17
+ */
18
+ import { decodeSymbol, WRUN_SYMBOL_CODE_MAX_CHARS } from './wrun-symbol-code.util.js';
19
+ /** The longest option root a contract names (the OSI root field). */
20
+ export const WRUN_ROW_MARKET_ROOT_MAX_CHARS = 6;
21
+ /** A contract's strike stays below this (eight digits of thousandths). */
22
+ export const WRUN_ROW_MARKET_STRIKE_LIMIT = 100_000;
23
+ export const WRUN_ROW_MARKET_MESSAGE = 'a market is null, { "contract": [root_code, expiry_ms, side_cp, strike] } or { "ticker": code }';
24
+ export const WRUN_ROW_MARKET_CONTRACT_MESSAGE = "a contract is [root_code, expiry_ms, side_cp, strike]: the option root's symbol code (1 to 6 characters), the expiry in epoch milliseconds, +1 for a call or -1 for a put, and a strike above 0 and below 100000";
25
+ export const WRUN_ROW_MARKET_TICKER_MESSAGE = 'a ticker is a symbol code of 1 to 10 characters';
26
+ function symbolCode(value, maxChars) {
27
+ if (typeof value !== 'number')
28
+ return false;
29
+ const text = decodeSymbol(value);
30
+ return text !== undefined && text.length <= maxChars;
31
+ }
32
+ function contractValues(value) {
33
+ if (!Array.isArray(value) || value.length !== 4)
34
+ return false;
35
+ const [root, expiryMs, sideCp, strike] = value;
36
+ return (symbolCode(root, WRUN_ROW_MARKET_ROOT_MAX_CHARS) &&
37
+ typeof expiryMs === 'number' &&
38
+ Number.isSafeInteger(expiryMs) &&
39
+ expiryMs > 0 &&
40
+ (sideCp === 1 || sideCp === -1) &&
41
+ typeof strike === 'number' &&
42
+ Number.isFinite(strike) &&
43
+ strike > 0 &&
44
+ strike < WRUN_ROW_MARKET_STRIKE_LIMIT);
45
+ }
46
+ /** One non-null entry's refusal (its path below the entry and the kit's words), or null when it
47
+ * names a market. */
48
+ export function wrunRowMarketIssue(value) {
49
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
50
+ return { path: [], message: WRUN_ROW_MARKET_MESSAGE };
51
+ }
52
+ const keys = Object.keys(value);
53
+ const entry = value;
54
+ if (keys.length !== 1 || (keys[0] !== 'contract' && keys[0] !== 'ticker')) {
55
+ return { path: [], message: WRUN_ROW_MARKET_MESSAGE };
56
+ }
57
+ if (keys[0] === 'contract') {
58
+ return contractValues(entry.contract) ? null : { path: ['contract'], message: WRUN_ROW_MARKET_CONTRACT_MESSAGE };
59
+ }
60
+ return symbolCode(entry.ticker, WRUN_SYMBOL_CODE_MAX_CHARS)
61
+ ? null
62
+ : { path: ['ticker'], message: WRUN_ROW_MARKET_TICKER_MESSAGE };
63
+ }
64
+ /** The refusal when `markets` and the rows it names differ in length (the kit's words). */
65
+ export function wrunRowMarketsLengthMessage(noun, count) {
66
+ return `markets must hold one entry per ${noun} (${count}), null for a ${noun} about no market`;
67
+ }
68
+ /** A frame's `markets`, checked against its row or line count: the list as written (each entry a
69
+ * copy), or the refusal as `[path, message]` (the path below the frame). */
70
+ export function parseWrunRowMarkets(value, noun, count) {
71
+ if (!Array.isArray(value)) {
72
+ // zod's words for a value that is no list (a decoded frame holds JSON types only).
73
+ const received = value === null ? 'null' : typeof value;
74
+ return { path: ['markets'], message: `Invalid input: expected array, received ${received}` };
75
+ }
76
+ const markets = [];
77
+ for (let at = 0; at < value.length; at += 1) {
78
+ const entry = value[at];
79
+ if (entry === null) {
80
+ markets.push(null);
81
+ continue;
82
+ }
83
+ const issue = wrunRowMarketIssue(entry);
84
+ if (issue)
85
+ return { path: ['markets', at, ...issue.path], message: issue.message };
86
+ const market = entry;
87
+ markets.push('contract' in market ? { contract: [...market.contract] } : { ticker: market.ticker });
88
+ }
89
+ if (markets.length !== count)
90
+ return { path: ['markets'], message: wrunRowMarketsLengthMessage(noun, count) };
91
+ return { markets };
92
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The symbol codec of the wrun options source classes (UW-PARITY source contract v2, global rule 3):
3
+ * a ticker or an option root travels inside a celled tuple as ONE f64.
4
+ *
5
+ * - Alphabet: `0-9` are 1 to 10, `A-Z` 11 to 36, `.` 37 (a character's value is its index in
6
+ * `WRUN_SYMBOL_CODE_ALPHABET` plus one, so no character is worth 0 and two texts never share a
7
+ * code).
8
+ * - code = sum of c_i x 38^(n-1-i) over the text's n characters, 1 to 10 characters: the largest
9
+ * code (ten dots) is 38^10 - 1 = 6,278,211,847,988,223, below 2^53, so every code is an exact f64.
10
+ * - Anything else is REFUSED, never truncated: an empty text, more than 10 characters, a character
11
+ * outside the alphabet (lower case included), a code that is not a whole number from 1 to the
12
+ * largest one, or a code with a zero digit in base 38 (no text encodes to it). A refusal answers
13
+ * `undefined`, the om kit's own convention (`wrunEncodeSymbol` / `wrunDecodeSymbol` in
14
+ * wrun-option-cells.ts), so the chart and the kit answer the same inputs the same way.
15
+ *
16
+ * A contract is (root_code, expiry_ms, side_cp, strike): `root_code` is `encodeSymbol(root)`.
17
+ * Pure, import-free: a lane module, a worker twin and a spec read it alike.
18
+ */
19
+ /** The characters a symbol code carries, in value order (value = index + 1). */
20
+ export declare const WRUN_SYMBOL_CODE_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.";
21
+ /** The base: one more than the largest character value. */
22
+ export declare const WRUN_SYMBOL_CODE_BASE = 38;
23
+ /** The longest symbol a code carries. */
24
+ export declare const WRUN_SYMBOL_CODE_MAX_CHARS = 10;
25
+ /** The largest code: ten dots, 38^10 - 1. */
26
+ export declare const WRUN_SYMBOL_CODE_MAX = 6278211847988223;
27
+ /** A ticker or an option root as one f64, or `undefined` when the text is refused (see the module
28
+ * doc): never a truncated or a lower-cased reading of it. */
29
+ export declare function encodeSymbol(text: string): number | undefined;
30
+ /** The text of a symbol code (`encodeSymbol`'s inverse), or `undefined` for anything that is no
31
+ * code (see the module doc). */
32
+ export declare function decodeSymbol(code: number): string | undefined;
@@ -0,0 +1,56 @@
1
+ /**
2
+ * The symbol codec of the wrun options source classes (UW-PARITY source contract v2, global rule 3):
3
+ * a ticker or an option root travels inside a celled tuple as ONE f64.
4
+ *
5
+ * - Alphabet: `0-9` are 1 to 10, `A-Z` 11 to 36, `.` 37 (a character's value is its index in
6
+ * `WRUN_SYMBOL_CODE_ALPHABET` plus one, so no character is worth 0 and two texts never share a
7
+ * code).
8
+ * - code = sum of c_i x 38^(n-1-i) over the text's n characters, 1 to 10 characters: the largest
9
+ * code (ten dots) is 38^10 - 1 = 6,278,211,847,988,223, below 2^53, so every code is an exact f64.
10
+ * - Anything else is REFUSED, never truncated: an empty text, more than 10 characters, a character
11
+ * outside the alphabet (lower case included), a code that is not a whole number from 1 to the
12
+ * largest one, or a code with a zero digit in base 38 (no text encodes to it). A refusal answers
13
+ * `undefined`, the om kit's own convention (`wrunEncodeSymbol` / `wrunDecodeSymbol` in
14
+ * wrun-option-cells.ts), so the chart and the kit answer the same inputs the same way.
15
+ *
16
+ * A contract is (root_code, expiry_ms, side_cp, strike): `root_code` is `encodeSymbol(root)`.
17
+ * Pure, import-free: a lane module, a worker twin and a spec read it alike.
18
+ */
19
+ /** The characters a symbol code carries, in value order (value = index + 1). */
20
+ export const WRUN_SYMBOL_CODE_ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.';
21
+ /** The base: one more than the largest character value. */
22
+ export const WRUN_SYMBOL_CODE_BASE = 38;
23
+ /** The longest symbol a code carries. */
24
+ export const WRUN_SYMBOL_CODE_MAX_CHARS = 10;
25
+ /** The largest code: ten dots, 38^10 - 1. */
26
+ export const WRUN_SYMBOL_CODE_MAX = 6_278_211_847_988_223;
27
+ /** A ticker or an option root as one f64, or `undefined` when the text is refused (see the module
28
+ * doc): never a truncated or a lower-cased reading of it. */
29
+ export function encodeSymbol(text) {
30
+ if (typeof text !== 'string' || text.length < 1 || text.length > WRUN_SYMBOL_CODE_MAX_CHARS)
31
+ return undefined;
32
+ let code = 0;
33
+ for (let at = 0; at < text.length; at += 1) {
34
+ const value = WRUN_SYMBOL_CODE_ALPHABET.indexOf(text.charAt(at)) + 1;
35
+ if (value < 1)
36
+ return undefined;
37
+ code = code * WRUN_SYMBOL_CODE_BASE + value;
38
+ }
39
+ return code;
40
+ }
41
+ /** The text of a symbol code (`encodeSymbol`'s inverse), or `undefined` for anything that is no
42
+ * code (see the module doc). */
43
+ export function decodeSymbol(code) {
44
+ if (typeof code !== 'number' || !Number.isInteger(code) || code < 1 || code > WRUN_SYMBOL_CODE_MAX)
45
+ return undefined;
46
+ let rest = code;
47
+ let text = '';
48
+ while (rest > 0) {
49
+ const value = rest % WRUN_SYMBOL_CODE_BASE;
50
+ if (value === 0)
51
+ return undefined;
52
+ text = WRUN_SYMBOL_CODE_ALPHABET.charAt(value - 1) + text;
53
+ rest = (rest - value) / WRUN_SYMBOL_CODE_BASE;
54
+ }
55
+ return text;
56
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orangecharts/wrun-chart-host",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "The wrun chart-host layer shared by the kiyotaka web app and the mobile app: the wrun result reader and painter model, cut from the web app (its reference implementation).",
5
5
  "type": "module",
6
6
  "files": [
@@ -24,12 +24,12 @@
24
24
  "typecheck": "tsc -p tsconfig.json --noEmit"
25
25
  },
26
26
  "peerDependencies": {
27
- "@orangecharts/indicator-kit": "0.78.1",
27
+ "@orangecharts/indicator-kit": "0.80.0",
28
28
  "dayjs": "1.11.23",
29
29
  "lodash-es": "4.18.1"
30
30
  },
31
31
  "devDependencies": {
32
- "@orangecharts/indicator-kit": "0.78.1",
32
+ "@orangecharts/indicator-kit": "0.80.0",
33
33
  "@types/lodash-es": "4.17.12",
34
34
  "dayjs": "1.11.23",
35
35
  "lodash-es": "4.18.1",