@purescraps/purehtml 0.9.0 → 0.10.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/examples/simple.js +229 -30
- package/dist/examples/simple.js.map +1 -1
- package/dist/src/index.d.ts +7 -0
- package/dist/src/index.js +229 -30
- package/dist/src/index.js.map +1 -1
- package/package.json +1 -1
- package/src/config-schema.json +14 -2
package/dist/src/index.d.ts
CHANGED
|
@@ -58,6 +58,13 @@ declare abstract class Config<T = unknown> {
|
|
|
58
58
|
declare class ConfigFactory {
|
|
59
59
|
static fromYAML<T>(yaml: string): Config<T>;
|
|
60
60
|
private static generate;
|
|
61
|
+
/**
|
|
62
|
+
* `type: number` / `type: boolean` on a primitive value are sugar for
|
|
63
|
+
* appending the corresponding transformer to the end of the transform
|
|
64
|
+
* chain, so the extracted value is coerced even when no explicit
|
|
65
|
+
* `transform` is provided. `type: string` (or no type) is left as-is.
|
|
66
|
+
*/
|
|
67
|
+
private static appendTypeCoercion;
|
|
61
68
|
private static generateSelector;
|
|
62
69
|
private static generateTransform;
|
|
63
70
|
private static detectExpectedType;
|
package/dist/src/index.js
CHANGED
|
@@ -6813,6 +6813,145 @@ var JSONTransformer = class extends Transformer {
|
|
|
6813
6813
|
}
|
|
6814
6814
|
};
|
|
6815
6815
|
|
|
6816
|
+
// src/transformers/jsonpath.ts
|
|
6817
|
+
var isIdentChar = (ch) => /[A-Za-z0-9_]/.test(ch);
|
|
6818
|
+
function parseJsonPath(path) {
|
|
6819
|
+
if (path.length === 0 || path[0] !== "$") {
|
|
6820
|
+
throw new Error(
|
|
6821
|
+
`jsonpath: invalid path ${JSON.stringify(path)}: must start with "$"`
|
|
6822
|
+
);
|
|
6823
|
+
}
|
|
6824
|
+
const segments = [];
|
|
6825
|
+
const n = path.length;
|
|
6826
|
+
let i = 1;
|
|
6827
|
+
while (i < n) {
|
|
6828
|
+
const ch = path[i];
|
|
6829
|
+
if (ch === ".") {
|
|
6830
|
+
i += 1;
|
|
6831
|
+
const start = i;
|
|
6832
|
+
while (i < n && isIdentChar(path[i])) {
|
|
6833
|
+
i += 1;
|
|
6834
|
+
}
|
|
6835
|
+
if (i === start) {
|
|
6836
|
+
throw new Error(
|
|
6837
|
+
`jsonpath: invalid path ${JSON.stringify(path)}: expected a name after "."`
|
|
6838
|
+
);
|
|
6839
|
+
}
|
|
6840
|
+
segments.push({ type: "key", key: path.slice(start, i) });
|
|
6841
|
+
continue;
|
|
6842
|
+
}
|
|
6843
|
+
if (ch === "[") {
|
|
6844
|
+
i += 1;
|
|
6845
|
+
if (i >= n) {
|
|
6846
|
+
throw new Error(
|
|
6847
|
+
`jsonpath: invalid path ${JSON.stringify(path)}: unterminated "["`
|
|
6848
|
+
);
|
|
6849
|
+
}
|
|
6850
|
+
const next = path[i];
|
|
6851
|
+
if (next === '"' || next === "'") {
|
|
6852
|
+
const quote = next;
|
|
6853
|
+
i += 1;
|
|
6854
|
+
let key = "";
|
|
6855
|
+
while (i < n && path[i] !== quote) {
|
|
6856
|
+
if (path[i] === "\\" && i + 1 < n && (path[i + 1] === quote || path[i + 1] === "\\")) {
|
|
6857
|
+
key += path[i + 1];
|
|
6858
|
+
i += 2;
|
|
6859
|
+
} else {
|
|
6860
|
+
key += path[i];
|
|
6861
|
+
i += 1;
|
|
6862
|
+
}
|
|
6863
|
+
}
|
|
6864
|
+
if (i >= n) {
|
|
6865
|
+
throw new Error(
|
|
6866
|
+
`jsonpath: invalid path ${JSON.stringify(path)}: unterminated quoted key`
|
|
6867
|
+
);
|
|
6868
|
+
}
|
|
6869
|
+
i += 1;
|
|
6870
|
+
segments.push({ type: "key", key });
|
|
6871
|
+
} else if (next === "-" || next >= "0" && next <= "9") {
|
|
6872
|
+
const start = i;
|
|
6873
|
+
if (next === "-") {
|
|
6874
|
+
i += 1;
|
|
6875
|
+
}
|
|
6876
|
+
const digitsStart = i;
|
|
6877
|
+
while (i < n && path[i] >= "0" && path[i] <= "9") {
|
|
6878
|
+
i += 1;
|
|
6879
|
+
}
|
|
6880
|
+
if (i === digitsStart) {
|
|
6881
|
+
throw new Error(
|
|
6882
|
+
`jsonpath: invalid path ${JSON.stringify(path)}: expected an index inside "[]"`
|
|
6883
|
+
);
|
|
6884
|
+
}
|
|
6885
|
+
segments.push({
|
|
6886
|
+
type: "index",
|
|
6887
|
+
index: parseInt(path.slice(start, i), 10)
|
|
6888
|
+
});
|
|
6889
|
+
} else {
|
|
6890
|
+
throw new Error(
|
|
6891
|
+
`jsonpath: invalid path ${JSON.stringify(path)}: expected an index or quoted key inside "[]"`
|
|
6892
|
+
);
|
|
6893
|
+
}
|
|
6894
|
+
if (path[i] !== "]") {
|
|
6895
|
+
throw new Error(
|
|
6896
|
+
`jsonpath: invalid path ${JSON.stringify(path)}: expected "]"`
|
|
6897
|
+
);
|
|
6898
|
+
}
|
|
6899
|
+
i += 1;
|
|
6900
|
+
continue;
|
|
6901
|
+
}
|
|
6902
|
+
throw new Error(
|
|
6903
|
+
`jsonpath: invalid path ${JSON.stringify(path)}: unexpected character "${ch}" at position ${i}`
|
|
6904
|
+
);
|
|
6905
|
+
}
|
|
6906
|
+
return segments;
|
|
6907
|
+
}
|
|
6908
|
+
function evaluateJsonPath(root, segments) {
|
|
6909
|
+
let current = root;
|
|
6910
|
+
for (const segment of segments) {
|
|
6911
|
+
if (current === null || current === void 0) {
|
|
6912
|
+
return null;
|
|
6913
|
+
}
|
|
6914
|
+
if (segment.type === "key") {
|
|
6915
|
+
if (typeof current !== "object" || Array.isArray(current)) {
|
|
6916
|
+
return null;
|
|
6917
|
+
}
|
|
6918
|
+
current = current[segment.key];
|
|
6919
|
+
} else {
|
|
6920
|
+
if (!Array.isArray(current)) {
|
|
6921
|
+
return null;
|
|
6922
|
+
}
|
|
6923
|
+
const index = segment.index < 0 ? current.length + segment.index : segment.index;
|
|
6924
|
+
current = index >= 0 && index < current.length ? current[index] : void 0;
|
|
6925
|
+
}
|
|
6926
|
+
}
|
|
6927
|
+
return current === void 0 ? null : current;
|
|
6928
|
+
}
|
|
6929
|
+
var JSONPathTransformer = class extends Transformer {
|
|
6930
|
+
constructor(args) {
|
|
6931
|
+
super();
|
|
6932
|
+
this.args = args;
|
|
6933
|
+
}
|
|
6934
|
+
static getName() {
|
|
6935
|
+
return "jsonpath";
|
|
6936
|
+
}
|
|
6937
|
+
inputType() {
|
|
6938
|
+
return STRING;
|
|
6939
|
+
}
|
|
6940
|
+
outputType() {
|
|
6941
|
+
return STRING;
|
|
6942
|
+
}
|
|
6943
|
+
transform({ val }) {
|
|
6944
|
+
if (val === null || val === void 0) {
|
|
6945
|
+
return null;
|
|
6946
|
+
}
|
|
6947
|
+
const [path] = this.args;
|
|
6948
|
+
if (path === void 0) {
|
|
6949
|
+
throw new Error('jsonpath: requires a path, e.g. jsonpath("$.a.b")');
|
|
6950
|
+
}
|
|
6951
|
+
return evaluateJsonPath(val, parseJsonPath(path));
|
|
6952
|
+
}
|
|
6953
|
+
};
|
|
6954
|
+
|
|
6816
6955
|
// src/transformers/length.ts
|
|
6817
6956
|
var Length = class extends Transformer {
|
|
6818
6957
|
static getName() {
|
|
@@ -7152,6 +7291,7 @@ var Transformers = class _Transformers {
|
|
|
7152
7291
|
HTMLTransformer,
|
|
7153
7292
|
JoinTransformer,
|
|
7154
7293
|
JSONTransformer,
|
|
7294
|
+
JSONPathTransformer,
|
|
7155
7295
|
Length,
|
|
7156
7296
|
LowerTransformer,
|
|
7157
7297
|
NumberTransformer,
|
|
@@ -7287,6 +7427,22 @@ var ConstantConfig = class _ConstantConfig extends ConfigWithSelector {
|
|
|
7287
7427
|
}
|
|
7288
7428
|
};
|
|
7289
7429
|
|
|
7430
|
+
// src/config/types/default.ts
|
|
7431
|
+
var DefaultValueConfig = class _DefaultValueConfig extends Config {
|
|
7432
|
+
constructor(inner, defaultValue) {
|
|
7433
|
+
super();
|
|
7434
|
+
this.inner = inner;
|
|
7435
|
+
this.defaultValue = defaultValue;
|
|
7436
|
+
}
|
|
7437
|
+
extract(params) {
|
|
7438
|
+
const val = this.inner.extract(params);
|
|
7439
|
+
return val === null ? this.defaultValue : val;
|
|
7440
|
+
}
|
|
7441
|
+
static generate(inner, defaultValue) {
|
|
7442
|
+
return new _DefaultValueConfig(inner, defaultValue);
|
|
7443
|
+
}
|
|
7444
|
+
};
|
|
7445
|
+
|
|
7290
7446
|
// src/config/types/object.ts
|
|
7291
7447
|
var ObjectConfig = class _ObjectConfig extends ConfigWithSelector {
|
|
7292
7448
|
static generate(selector, properties) {
|
|
@@ -7424,7 +7580,7 @@ var oneOf = [
|
|
|
7424
7580
|
},
|
|
7425
7581
|
type: {
|
|
7426
7582
|
type: "string",
|
|
7427
|
-
enum: ["string", "object", "array", "union"]
|
|
7583
|
+
enum: ["string", "number", "boolean", "object", "array", "union"]
|
|
7428
7584
|
},
|
|
7429
7585
|
items: { $ref: "config-schema.json" },
|
|
7430
7586
|
properties: {
|
|
@@ -7446,6 +7602,9 @@ var oneOf = [
|
|
|
7446
7602
|
minItems: 1
|
|
7447
7603
|
}
|
|
7448
7604
|
]
|
|
7605
|
+
},
|
|
7606
|
+
default: {
|
|
7607
|
+
$comment: "fallback value returned in place of null when the selector matches nothing"
|
|
7449
7608
|
}
|
|
7450
7609
|
},
|
|
7451
7610
|
required: [],
|
|
@@ -7505,6 +7664,9 @@ var oneOf = [
|
|
|
7505
7664
|
type: "array",
|
|
7506
7665
|
items: { $ref: "config-schema.json" },
|
|
7507
7666
|
minItems: 1
|
|
7667
|
+
},
|
|
7668
|
+
default: {
|
|
7669
|
+
$comment: "fallback value returned in place of null when no union member matches"
|
|
7508
7670
|
}
|
|
7509
7671
|
},
|
|
7510
7672
|
required: ["union"],
|
|
@@ -7513,7 +7675,13 @@ var oneOf = [
|
|
|
7513
7675
|
{
|
|
7514
7676
|
$comment: "Constant",
|
|
7515
7677
|
type: "object",
|
|
7516
|
-
properties: {
|
|
7678
|
+
properties: {
|
|
7679
|
+
selector: { type: "string" },
|
|
7680
|
+
constant: {},
|
|
7681
|
+
default: {
|
|
7682
|
+
$comment: "fallback value returned in place of null when the selector matches nothing"
|
|
7683
|
+
}
|
|
7684
|
+
},
|
|
7517
7685
|
required: ["constant"],
|
|
7518
7686
|
additionalProperties: false
|
|
7519
7687
|
}
|
|
@@ -7560,41 +7728,72 @@ var ConfigFactory = class _ConfigFactory {
|
|
|
7560
7728
|
transform: transformOrig,
|
|
7561
7729
|
properties,
|
|
7562
7730
|
items,
|
|
7563
|
-
union
|
|
7731
|
+
union,
|
|
7732
|
+
default: defaultValue
|
|
7564
7733
|
} = plain;
|
|
7565
7734
|
const selector = _ConfigFactory.generateSelector(selectorOrig);
|
|
7566
7735
|
const expectedType = _ConfigFactory.detectExpectedType(plain);
|
|
7567
7736
|
const transform = transformOrig ? _ConfigFactory.generateTransform(transformOrig) : void 0;
|
|
7568
|
-
|
|
7569
|
-
|
|
7570
|
-
|
|
7571
|
-
|
|
7572
|
-
|
|
7573
|
-
|
|
7574
|
-
|
|
7575
|
-
(
|
|
7576
|
-
acc
|
|
7577
|
-
|
|
7578
|
-
|
|
7579
|
-
|
|
7580
|
-
|
|
7737
|
+
const config = (() => {
|
|
7738
|
+
switch (expectedType) {
|
|
7739
|
+
case "constant":
|
|
7740
|
+
return ConstantConfig.generate(constant, selector);
|
|
7741
|
+
case "object": {
|
|
7742
|
+
let propConfigs;
|
|
7743
|
+
if (properties) {
|
|
7744
|
+
propConfigs = Object.keys(properties).reduce(
|
|
7745
|
+
(acc, key) => {
|
|
7746
|
+
acc[key] = _ConfigFactory.generate(properties[key]);
|
|
7747
|
+
return acc;
|
|
7748
|
+
},
|
|
7749
|
+
{}
|
|
7750
|
+
);
|
|
7751
|
+
}
|
|
7752
|
+
return ObjectConfig.generate(selector, propConfigs);
|
|
7581
7753
|
}
|
|
7582
|
-
|
|
7754
|
+
case "array":
|
|
7755
|
+
return ArrayConfig.generate(
|
|
7756
|
+
selector,
|
|
7757
|
+
items && _ConfigFactory.generate(items),
|
|
7758
|
+
transform
|
|
7759
|
+
);
|
|
7760
|
+
case "union":
|
|
7761
|
+
return UnionConfig.generate(
|
|
7762
|
+
// biome-ignore lint/style/noNonNullAssertion: union will be present here. because our schema already validated the input
|
|
7763
|
+
union.map((cfg) => _ConfigFactory.generate(cfg))
|
|
7764
|
+
);
|
|
7765
|
+
default:
|
|
7766
|
+
return PrimitiveValueConfig.generate(
|
|
7767
|
+
selector,
|
|
7768
|
+
_ConfigFactory.appendTypeCoercion(plain.type, transform)
|
|
7769
|
+
);
|
|
7583
7770
|
}
|
|
7584
|
-
|
|
7585
|
-
|
|
7586
|
-
|
|
7587
|
-
|
|
7588
|
-
|
|
7589
|
-
|
|
7590
|
-
case "union":
|
|
7591
|
-
return UnionConfig.generate(
|
|
7592
|
-
// biome-ignore lint/style/noNonNullAssertion: union will be present here. because our schema already validated the input
|
|
7593
|
-
union.map((cfg) => _ConfigFactory.generate(cfg))
|
|
7594
|
-
);
|
|
7595
|
-
default:
|
|
7596
|
-
return PrimitiveValueConfig.generate(selector, transform);
|
|
7771
|
+
})();
|
|
7772
|
+
if (defaultValue !== void 0) {
|
|
7773
|
+
return DefaultValueConfig.generate(
|
|
7774
|
+
config,
|
|
7775
|
+
defaultValue
|
|
7776
|
+
);
|
|
7597
7777
|
}
|
|
7778
|
+
return config;
|
|
7779
|
+
}
|
|
7780
|
+
/**
|
|
7781
|
+
* `type: number` / `type: boolean` on a primitive value are sugar for
|
|
7782
|
+
* appending the corresponding transformer to the end of the transform
|
|
7783
|
+
* chain, so the extracted value is coerced even when no explicit
|
|
7784
|
+
* `transform` is provided. `type: string` (or no type) is left as-is.
|
|
7785
|
+
*/
|
|
7786
|
+
static appendTypeCoercion(type2, transform) {
|
|
7787
|
+
if (type2 !== "number" && type2 !== "boolean") {
|
|
7788
|
+
return transform;
|
|
7789
|
+
}
|
|
7790
|
+
const coercion = TransformerFactory.create(type2);
|
|
7791
|
+
if (!transform) {
|
|
7792
|
+
return coercion;
|
|
7793
|
+
}
|
|
7794
|
+
return (Array.isArray(transform) ? transform : [transform]).concat(
|
|
7795
|
+
coercion
|
|
7796
|
+
);
|
|
7598
7797
|
}
|
|
7599
7798
|
static generateSelector(selector) {
|
|
7600
7799
|
if (typeof selector === "string") {
|