oakscriptjs 0.21.0 → 0.22.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/dist/cjs/check/spec.d.ts +1 -1
- package/dist/cjs/check/spec.d.ts.map +1 -1
- package/dist/cjs/check/spec.js +1 -1
- package/dist/cjs/check/spec.js.map +2 -2
- package/dist/cjs/runtime/types.d.ts +9 -4
- package/dist/cjs/runtime/types.d.ts.map +1 -1
- package/dist/cjs/runtime/types.js.map +1 -1
- package/dist/cjs/script/globals.d.ts +1 -0
- package/dist/cjs/script/globals.d.ts.map +1 -1
- package/dist/cjs/script/globals.js.map +1 -1
- package/dist/cjs/script/index.d.ts +26 -0
- package/dist/cjs/script/index.d.ts.map +1 -1
- package/dist/cjs/script/index.js +59 -3
- package/dist/cjs/script/index.js.map +2 -2
- package/dist/esm/check/spec.d.ts +1 -1
- package/dist/esm/check/spec.d.ts.map +1 -1
- package/dist/esm/check/spec.js +1 -1
- package/dist/esm/check/spec.js.map +1 -1
- package/dist/esm/runtime/types.d.ts +9 -4
- package/dist/esm/runtime/types.d.ts.map +1 -1
- package/dist/esm/script/globals.d.ts +1 -0
- package/dist/esm/script/globals.d.ts.map +1 -1
- package/dist/esm/script/globals.js.map +1 -1
- package/dist/esm/script/index.d.ts +26 -0
- package/dist/esm/script/index.d.ts.map +1 -1
- package/dist/esm/script/index.js +67 -3
- package/dist/esm/script/index.js.map +1 -1
- package/package.json +1 -1
- package/skills/oakscript/reference/script-structure.md +12 -2
- package/skills/oakscript/spec.schema.json +2 -1
- package/src/check/spec.ts +1 -1
- package/src/runtime/types.ts +9 -4
- package/src/script/globals.ts +1 -0
- package/src/script/index.ts +79 -3
package/dist/cjs/script/index.js
CHANGED
|
@@ -53,6 +53,7 @@ __export(script_exports, {
|
|
|
53
53
|
currency: () => currency,
|
|
54
54
|
dayofmonth: () => dayofmonth,
|
|
55
55
|
dayofweek: () => dayofweek,
|
|
56
|
+
defineEnum: () => defineEnum,
|
|
56
57
|
display: () => display,
|
|
57
58
|
dividends: () => dividends,
|
|
58
59
|
eachBar: () => eachBar,
|
|
@@ -716,6 +717,34 @@ function declare(c, kind) {
|
|
|
716
717
|
}
|
|
717
718
|
c.declaration = kind;
|
|
718
719
|
}
|
|
720
|
+
class EnumFieldValue {
|
|
721
|
+
constructor(name, title) {
|
|
722
|
+
this.name = name;
|
|
723
|
+
this.title = title;
|
|
724
|
+
Object.freeze(this);
|
|
725
|
+
}
|
|
726
|
+
name;
|
|
727
|
+
title;
|
|
728
|
+
toString() {
|
|
729
|
+
return this.title;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
const enumFieldsOf = /* @__PURE__ */ new WeakMap();
|
|
733
|
+
function defineEnum(fields) {
|
|
734
|
+
if (!(0, import_drawing_args.isPlainObject)(fields) || Object.keys(fields).length === 0) {
|
|
735
|
+
throw new TypeError('defineEnum(): the fields are an object of names and titles, e.g. { sma: "Simple", ema: "Exponential" }.');
|
|
736
|
+
}
|
|
737
|
+
const list = [];
|
|
738
|
+
const out = {};
|
|
739
|
+
for (const [name, title] of Object.entries(fields)) {
|
|
740
|
+
if (title !== void 0 && typeof title !== "string") throw new TypeError(`defineEnum(): the title of "${name}" is a string.`);
|
|
741
|
+
const field = new EnumFieldValue(name, title || name);
|
|
742
|
+
list.push(field);
|
|
743
|
+
out[name] = field;
|
|
744
|
+
}
|
|
745
|
+
for (const field of list) enumFieldsOf.set(field, list);
|
|
746
|
+
return Object.freeze(out);
|
|
747
|
+
}
|
|
719
748
|
function common(opts) {
|
|
720
749
|
const out = {};
|
|
721
750
|
if (opts.group !== void 0) out.group = opts.group;
|
|
@@ -736,15 +765,16 @@ function registerInput(config, fallbackId) {
|
|
|
736
765
|
}
|
|
737
766
|
function registerNumeric(type, defval, title, opts = {}) {
|
|
738
767
|
const v = registerInput(
|
|
739
|
-
{ type, defval, title, min: opts.minval, max: opts.maxval, step: opts.step, ...common(opts) },
|
|
768
|
+
{ type, defval, title, min: opts.minval, max: opts.maxval, step: opts.step, ...opts.options ? { options: opts.options } : {}, ...common(opts) },
|
|
740
769
|
`input_${type}`
|
|
741
770
|
);
|
|
742
771
|
return typeof v === "number" ? v : defval;
|
|
743
772
|
}
|
|
744
773
|
const COMMON_INPUT_KEYS = ["tooltip", "inline", "group", "confirm", "display", "active"];
|
|
745
774
|
const INPUT_KEYS = {
|
|
746
|
-
int: ["defval", "title", "minval", "maxval", "step", ...COMMON_INPUT_KEYS],
|
|
747
|
-
float: ["defval", "title", "minval", "maxval", "step", ...COMMON_INPUT_KEYS],
|
|
775
|
+
int: ["defval", "title", "minval", "maxval", "step", "options", ...COMMON_INPUT_KEYS],
|
|
776
|
+
float: ["defval", "title", "minval", "maxval", "step", "options", ...COMMON_INPUT_KEYS],
|
|
777
|
+
enum: ["defval", "title", "options", ...COMMON_INPUT_KEYS],
|
|
748
778
|
price: ["defval", "title", ...COMMON_INPUT_KEYS],
|
|
749
779
|
bool: ["defval", "title", ...COMMON_INPUT_KEYS],
|
|
750
780
|
string: ["defval", "title", "options", ...COMMON_INPUT_KEYS],
|
|
@@ -841,6 +871,32 @@ const input = strictNamespace("input", {
|
|
|
841
871
|
const a = inputArgs("price", defval, title, opts);
|
|
842
872
|
return registerNumeric("float", a.defval, a.title, a.opts);
|
|
843
873
|
},
|
|
874
|
+
/**
|
|
875
|
+
* A field of an enum made with `defineEnum()`: `input.enum(MaType.sma, "Average")`. The host lists the fields by
|
|
876
|
+
* title (`options`: only these fields) and stores the field name; the call returns the field.
|
|
877
|
+
*/
|
|
878
|
+
enum(defval, title, opts) {
|
|
879
|
+
const a = inputArgs("enum", defval, title, opts);
|
|
880
|
+
const fields = a.defval === void 0 ? void 0 : enumFieldsOf.get(a.defval);
|
|
881
|
+
if (!fields) throw new TypeError('input.enum(): the default is a field of an enum made with defineEnum(), e.g. input.enum(MaType.sma, "Average").');
|
|
882
|
+
const choices = a.opts.options ?? fields;
|
|
883
|
+
for (const f of choices) {
|
|
884
|
+
if (!fields.includes(f)) throw new TypeError("input.enum(): the options are fields of the enum of the default.");
|
|
885
|
+
}
|
|
886
|
+
if (!choices.includes(a.defval)) throw new TypeError("input.enum(): the default is one of the options.");
|
|
887
|
+
const name = registerInput(
|
|
888
|
+
{
|
|
889
|
+
type: "enum",
|
|
890
|
+
defval: a.defval.name,
|
|
891
|
+
title: a.title,
|
|
892
|
+
options: choices.map((f) => f.name),
|
|
893
|
+
optionsTitles: Object.fromEntries(choices.map((f) => [f.name, f.title])),
|
|
894
|
+
...common(a.opts)
|
|
895
|
+
},
|
|
896
|
+
"input_enum"
|
|
897
|
+
);
|
|
898
|
+
return choices.find((f) => f.name === name) ?? a.defval;
|
|
899
|
+
},
|
|
844
900
|
/** Date and time as a UNIX time in milliseconds, e.g. `input.time(timestamp('2024-01-01'), 'Start')`. */
|
|
845
901
|
time(defval, title, opts) {
|
|
846
902
|
const a = inputArgs("time", defval, title, opts);
|