@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/examples/simple.js
CHANGED
|
@@ -6802,6 +6802,145 @@ var JSONTransformer = class extends Transformer {
|
|
|
6802
6802
|
}
|
|
6803
6803
|
};
|
|
6804
6804
|
|
|
6805
|
+
// src/transformers/jsonpath.ts
|
|
6806
|
+
var isIdentChar = (ch) => /[A-Za-z0-9_]/.test(ch);
|
|
6807
|
+
function parseJsonPath(path) {
|
|
6808
|
+
if (path.length === 0 || path[0] !== "$") {
|
|
6809
|
+
throw new Error(
|
|
6810
|
+
`jsonpath: invalid path ${JSON.stringify(path)}: must start with "$"`
|
|
6811
|
+
);
|
|
6812
|
+
}
|
|
6813
|
+
const segments = [];
|
|
6814
|
+
const n = path.length;
|
|
6815
|
+
let i = 1;
|
|
6816
|
+
while (i < n) {
|
|
6817
|
+
const ch = path[i];
|
|
6818
|
+
if (ch === ".") {
|
|
6819
|
+
i += 1;
|
|
6820
|
+
const start = i;
|
|
6821
|
+
while (i < n && isIdentChar(path[i])) {
|
|
6822
|
+
i += 1;
|
|
6823
|
+
}
|
|
6824
|
+
if (i === start) {
|
|
6825
|
+
throw new Error(
|
|
6826
|
+
`jsonpath: invalid path ${JSON.stringify(path)}: expected a name after "."`
|
|
6827
|
+
);
|
|
6828
|
+
}
|
|
6829
|
+
segments.push({ type: "key", key: path.slice(start, i) });
|
|
6830
|
+
continue;
|
|
6831
|
+
}
|
|
6832
|
+
if (ch === "[") {
|
|
6833
|
+
i += 1;
|
|
6834
|
+
if (i >= n) {
|
|
6835
|
+
throw new Error(
|
|
6836
|
+
`jsonpath: invalid path ${JSON.stringify(path)}: unterminated "["`
|
|
6837
|
+
);
|
|
6838
|
+
}
|
|
6839
|
+
const next = path[i];
|
|
6840
|
+
if (next === '"' || next === "'") {
|
|
6841
|
+
const quote = next;
|
|
6842
|
+
i += 1;
|
|
6843
|
+
let key = "";
|
|
6844
|
+
while (i < n && path[i] !== quote) {
|
|
6845
|
+
if (path[i] === "\\" && i + 1 < n && (path[i + 1] === quote || path[i + 1] === "\\")) {
|
|
6846
|
+
key += path[i + 1];
|
|
6847
|
+
i += 2;
|
|
6848
|
+
} else {
|
|
6849
|
+
key += path[i];
|
|
6850
|
+
i += 1;
|
|
6851
|
+
}
|
|
6852
|
+
}
|
|
6853
|
+
if (i >= n) {
|
|
6854
|
+
throw new Error(
|
|
6855
|
+
`jsonpath: invalid path ${JSON.stringify(path)}: unterminated quoted key`
|
|
6856
|
+
);
|
|
6857
|
+
}
|
|
6858
|
+
i += 1;
|
|
6859
|
+
segments.push({ type: "key", key });
|
|
6860
|
+
} else if (next === "-" || next >= "0" && next <= "9") {
|
|
6861
|
+
const start = i;
|
|
6862
|
+
if (next === "-") {
|
|
6863
|
+
i += 1;
|
|
6864
|
+
}
|
|
6865
|
+
const digitsStart = i;
|
|
6866
|
+
while (i < n && path[i] >= "0" && path[i] <= "9") {
|
|
6867
|
+
i += 1;
|
|
6868
|
+
}
|
|
6869
|
+
if (i === digitsStart) {
|
|
6870
|
+
throw new Error(
|
|
6871
|
+
`jsonpath: invalid path ${JSON.stringify(path)}: expected an index inside "[]"`
|
|
6872
|
+
);
|
|
6873
|
+
}
|
|
6874
|
+
segments.push({
|
|
6875
|
+
type: "index",
|
|
6876
|
+
index: parseInt(path.slice(start, i), 10)
|
|
6877
|
+
});
|
|
6878
|
+
} else {
|
|
6879
|
+
throw new Error(
|
|
6880
|
+
`jsonpath: invalid path ${JSON.stringify(path)}: expected an index or quoted key inside "[]"`
|
|
6881
|
+
);
|
|
6882
|
+
}
|
|
6883
|
+
if (path[i] !== "]") {
|
|
6884
|
+
throw new Error(
|
|
6885
|
+
`jsonpath: invalid path ${JSON.stringify(path)}: expected "]"`
|
|
6886
|
+
);
|
|
6887
|
+
}
|
|
6888
|
+
i += 1;
|
|
6889
|
+
continue;
|
|
6890
|
+
}
|
|
6891
|
+
throw new Error(
|
|
6892
|
+
`jsonpath: invalid path ${JSON.stringify(path)}: unexpected character "${ch}" at position ${i}`
|
|
6893
|
+
);
|
|
6894
|
+
}
|
|
6895
|
+
return segments;
|
|
6896
|
+
}
|
|
6897
|
+
function evaluateJsonPath(root, segments) {
|
|
6898
|
+
let current = root;
|
|
6899
|
+
for (const segment of segments) {
|
|
6900
|
+
if (current === null || current === void 0) {
|
|
6901
|
+
return null;
|
|
6902
|
+
}
|
|
6903
|
+
if (segment.type === "key") {
|
|
6904
|
+
if (typeof current !== "object" || Array.isArray(current)) {
|
|
6905
|
+
return null;
|
|
6906
|
+
}
|
|
6907
|
+
current = current[segment.key];
|
|
6908
|
+
} else {
|
|
6909
|
+
if (!Array.isArray(current)) {
|
|
6910
|
+
return null;
|
|
6911
|
+
}
|
|
6912
|
+
const index = segment.index < 0 ? current.length + segment.index : segment.index;
|
|
6913
|
+
current = index >= 0 && index < current.length ? current[index] : void 0;
|
|
6914
|
+
}
|
|
6915
|
+
}
|
|
6916
|
+
return current === void 0 ? null : current;
|
|
6917
|
+
}
|
|
6918
|
+
var JSONPathTransformer = class extends Transformer {
|
|
6919
|
+
constructor(args) {
|
|
6920
|
+
super();
|
|
6921
|
+
this.args = args;
|
|
6922
|
+
}
|
|
6923
|
+
static getName() {
|
|
6924
|
+
return "jsonpath";
|
|
6925
|
+
}
|
|
6926
|
+
inputType() {
|
|
6927
|
+
return STRING;
|
|
6928
|
+
}
|
|
6929
|
+
outputType() {
|
|
6930
|
+
return STRING;
|
|
6931
|
+
}
|
|
6932
|
+
transform({ val }) {
|
|
6933
|
+
if (val === null || val === void 0) {
|
|
6934
|
+
return null;
|
|
6935
|
+
}
|
|
6936
|
+
const [path] = this.args;
|
|
6937
|
+
if (path === void 0) {
|
|
6938
|
+
throw new Error('jsonpath: requires a path, e.g. jsonpath("$.a.b")');
|
|
6939
|
+
}
|
|
6940
|
+
return evaluateJsonPath(val, parseJsonPath(path));
|
|
6941
|
+
}
|
|
6942
|
+
};
|
|
6943
|
+
|
|
6805
6944
|
// src/transformers/length.ts
|
|
6806
6945
|
var Length = class extends Transformer {
|
|
6807
6946
|
static getName() {
|
|
@@ -7141,6 +7280,7 @@ var Transformers = class _Transformers {
|
|
|
7141
7280
|
HTMLTransformer,
|
|
7142
7281
|
JoinTransformer,
|
|
7143
7282
|
JSONTransformer,
|
|
7283
|
+
JSONPathTransformer,
|
|
7144
7284
|
Length,
|
|
7145
7285
|
LowerTransformer,
|
|
7146
7286
|
NumberTransformer,
|
|
@@ -7276,6 +7416,22 @@ var ConstantConfig = class _ConstantConfig extends ConfigWithSelector {
|
|
|
7276
7416
|
}
|
|
7277
7417
|
};
|
|
7278
7418
|
|
|
7419
|
+
// src/config/types/default.ts
|
|
7420
|
+
var DefaultValueConfig = class _DefaultValueConfig extends Config {
|
|
7421
|
+
constructor(inner, defaultValue) {
|
|
7422
|
+
super();
|
|
7423
|
+
this.inner = inner;
|
|
7424
|
+
this.defaultValue = defaultValue;
|
|
7425
|
+
}
|
|
7426
|
+
extract(params) {
|
|
7427
|
+
const val = this.inner.extract(params);
|
|
7428
|
+
return val === null ? this.defaultValue : val;
|
|
7429
|
+
}
|
|
7430
|
+
static generate(inner, defaultValue) {
|
|
7431
|
+
return new _DefaultValueConfig(inner, defaultValue);
|
|
7432
|
+
}
|
|
7433
|
+
};
|
|
7434
|
+
|
|
7279
7435
|
// src/config/types/object.ts
|
|
7280
7436
|
var ObjectConfig = class _ObjectConfig extends ConfigWithSelector {
|
|
7281
7437
|
static generate(selector, properties) {
|
|
@@ -7413,7 +7569,7 @@ var oneOf = [
|
|
|
7413
7569
|
},
|
|
7414
7570
|
type: {
|
|
7415
7571
|
type: "string",
|
|
7416
|
-
enum: ["string", "object", "array", "union"]
|
|
7572
|
+
enum: ["string", "number", "boolean", "object", "array", "union"]
|
|
7417
7573
|
},
|
|
7418
7574
|
items: { $ref: "config-schema.json" },
|
|
7419
7575
|
properties: {
|
|
@@ -7435,6 +7591,9 @@ var oneOf = [
|
|
|
7435
7591
|
minItems: 1
|
|
7436
7592
|
}
|
|
7437
7593
|
]
|
|
7594
|
+
},
|
|
7595
|
+
default: {
|
|
7596
|
+
$comment: "fallback value returned in place of null when the selector matches nothing"
|
|
7438
7597
|
}
|
|
7439
7598
|
},
|
|
7440
7599
|
required: [],
|
|
@@ -7494,6 +7653,9 @@ var oneOf = [
|
|
|
7494
7653
|
type: "array",
|
|
7495
7654
|
items: { $ref: "config-schema.json" },
|
|
7496
7655
|
minItems: 1
|
|
7656
|
+
},
|
|
7657
|
+
default: {
|
|
7658
|
+
$comment: "fallback value returned in place of null when no union member matches"
|
|
7497
7659
|
}
|
|
7498
7660
|
},
|
|
7499
7661
|
required: ["union"],
|
|
@@ -7502,7 +7664,13 @@ var oneOf = [
|
|
|
7502
7664
|
{
|
|
7503
7665
|
$comment: "Constant",
|
|
7504
7666
|
type: "object",
|
|
7505
|
-
properties: {
|
|
7667
|
+
properties: {
|
|
7668
|
+
selector: { type: "string" },
|
|
7669
|
+
constant: {},
|
|
7670
|
+
default: {
|
|
7671
|
+
$comment: "fallback value returned in place of null when the selector matches nothing"
|
|
7672
|
+
}
|
|
7673
|
+
},
|
|
7506
7674
|
required: ["constant"],
|
|
7507
7675
|
additionalProperties: false
|
|
7508
7676
|
}
|
|
@@ -7549,41 +7717,72 @@ var ConfigFactory = class _ConfigFactory {
|
|
|
7549
7717
|
transform: transformOrig,
|
|
7550
7718
|
properties,
|
|
7551
7719
|
items,
|
|
7552
|
-
union
|
|
7720
|
+
union,
|
|
7721
|
+
default: defaultValue
|
|
7553
7722
|
} = plain;
|
|
7554
7723
|
const selector = _ConfigFactory.generateSelector(selectorOrig);
|
|
7555
7724
|
const expectedType = _ConfigFactory.detectExpectedType(plain);
|
|
7556
7725
|
const transform = transformOrig ? _ConfigFactory.generateTransform(transformOrig) : void 0;
|
|
7557
|
-
|
|
7558
|
-
|
|
7559
|
-
|
|
7560
|
-
|
|
7561
|
-
|
|
7562
|
-
|
|
7563
|
-
|
|
7564
|
-
(
|
|
7565
|
-
acc
|
|
7566
|
-
|
|
7567
|
-
|
|
7568
|
-
|
|
7569
|
-
|
|
7726
|
+
const config2 = (() => {
|
|
7727
|
+
switch (expectedType) {
|
|
7728
|
+
case "constant":
|
|
7729
|
+
return ConstantConfig.generate(constant, selector);
|
|
7730
|
+
case "object": {
|
|
7731
|
+
let propConfigs;
|
|
7732
|
+
if (properties) {
|
|
7733
|
+
propConfigs = Object.keys(properties).reduce(
|
|
7734
|
+
(acc, key) => {
|
|
7735
|
+
acc[key] = _ConfigFactory.generate(properties[key]);
|
|
7736
|
+
return acc;
|
|
7737
|
+
},
|
|
7738
|
+
{}
|
|
7739
|
+
);
|
|
7740
|
+
}
|
|
7741
|
+
return ObjectConfig.generate(selector, propConfigs);
|
|
7570
7742
|
}
|
|
7571
|
-
|
|
7743
|
+
case "array":
|
|
7744
|
+
return ArrayConfig.generate(
|
|
7745
|
+
selector,
|
|
7746
|
+
items && _ConfigFactory.generate(items),
|
|
7747
|
+
transform
|
|
7748
|
+
);
|
|
7749
|
+
case "union":
|
|
7750
|
+
return UnionConfig.generate(
|
|
7751
|
+
// biome-ignore lint/style/noNonNullAssertion: union will be present here. because our schema already validated the input
|
|
7752
|
+
union.map((cfg) => _ConfigFactory.generate(cfg))
|
|
7753
|
+
);
|
|
7754
|
+
default:
|
|
7755
|
+
return PrimitiveValueConfig.generate(
|
|
7756
|
+
selector,
|
|
7757
|
+
_ConfigFactory.appendTypeCoercion(plain.type, transform)
|
|
7758
|
+
);
|
|
7572
7759
|
}
|
|
7573
|
-
|
|
7574
|
-
|
|
7575
|
-
|
|
7576
|
-
|
|
7577
|
-
|
|
7578
|
-
|
|
7579
|
-
case "union":
|
|
7580
|
-
return UnionConfig.generate(
|
|
7581
|
-
// biome-ignore lint/style/noNonNullAssertion: union will be present here. because our schema already validated the input
|
|
7582
|
-
union.map((cfg) => _ConfigFactory.generate(cfg))
|
|
7583
|
-
);
|
|
7584
|
-
default:
|
|
7585
|
-
return PrimitiveValueConfig.generate(selector, transform);
|
|
7760
|
+
})();
|
|
7761
|
+
if (defaultValue !== void 0) {
|
|
7762
|
+
return DefaultValueConfig.generate(
|
|
7763
|
+
config2,
|
|
7764
|
+
defaultValue
|
|
7765
|
+
);
|
|
7586
7766
|
}
|
|
7767
|
+
return config2;
|
|
7768
|
+
}
|
|
7769
|
+
/**
|
|
7770
|
+
* `type: number` / `type: boolean` on a primitive value are sugar for
|
|
7771
|
+
* appending the corresponding transformer to the end of the transform
|
|
7772
|
+
* chain, so the extracted value is coerced even when no explicit
|
|
7773
|
+
* `transform` is provided. `type: string` (or no type) is left as-is.
|
|
7774
|
+
*/
|
|
7775
|
+
static appendTypeCoercion(type2, transform) {
|
|
7776
|
+
if (type2 !== "number" && type2 !== "boolean") {
|
|
7777
|
+
return transform;
|
|
7778
|
+
}
|
|
7779
|
+
const coercion = TransformerFactory.create(type2);
|
|
7780
|
+
if (!transform) {
|
|
7781
|
+
return coercion;
|
|
7782
|
+
}
|
|
7783
|
+
return (Array.isArray(transform) ? transform : [transform]).concat(
|
|
7784
|
+
coercion
|
|
7785
|
+
);
|
|
7587
7786
|
}
|
|
7588
7787
|
static generateSelector(selector) {
|
|
7589
7788
|
if (typeof selector === "string") {
|