@rdfx/literal 0.0.21 → 0.0.22

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.
@@ -1,4 +1,5 @@
1
1
  import type { Literal } from "@rdfjs/types";
2
+ import { Decimal } from "decimal.js";
2
3
  import { Either } from "purify-ts";
3
4
  import type { Primitive } from "./Primitive.js";
4
5
  /**
@@ -7,6 +8,7 @@ import type { Primitive } from "./Primitive.js";
7
8
  * Partially adapted from rdf-literal.js (https://github.com/rubensworks/rdf-literal.js), MIT license.
8
9
  */
9
10
  export declare namespace LiteralDecoder {
11
+ function decodeBigDecimalLiteral(literal: Literal): Either<Error, Decimal>;
10
12
  function decodeBigIntLiteral(literal: Literal): Either<Error, bigint>;
11
13
  function decodeBooleanLiteral(literal: Literal): Either<Error, boolean>;
12
14
  function decodeDateLiteral(literal: Literal): Either<Error, Date>;
@@ -1,3 +1,4 @@
1
+ import { Decimal } from "decimal.js";
1
2
  import { Either, Left } from "purify-ts";
2
3
  import { literalDatatypeDefinitions } from "./literalDatatypeDefinitions.js";
3
4
  /**
@@ -9,6 +10,19 @@ export var LiteralDecoder;
9
10
  (function (LiteralDecoder) {
10
11
  const BIGINT_NUMBER_MAX_SAFE_INTEGER = BigInt(Number.MAX_SAFE_INTEGER);
11
12
  const BIGINT_NUMBER_MIN_SAFE_INTEGER = BigInt(Number.MIN_SAFE_INTEGER);
13
+ function convertBigDecimalToBigInt(literal, value) {
14
+ if (value.isInt()) {
15
+ return Either.of(BigInt(value.toFixed(0)));
16
+ }
17
+ return Left(new LiteralValueError(literal, `Decimal ${value} is not an integer`));
18
+ }
19
+ function convertBigDecimalToNumber(literal, value) {
20
+ const valueToNumber = value.toNumber();
21
+ if (new Decimal(valueToNumber).equals(value)) {
22
+ return Either.of(valueToNumber);
23
+ }
24
+ return Left(new LiteralValueError(literal, `Decimal ${value} is outside number's safe range [-${Number.MAX_VALUE}, ${Number.MAX_VALUE}]`));
25
+ }
12
26
  function convertBigIntToNumber(literal, value) {
13
27
  if (value >= BIGINT_NUMBER_MIN_SAFE_INTEGER &&
14
28
  value <= BIGINT_NUMBER_MAX_SAFE_INTEGER) {
@@ -22,12 +36,36 @@ export var LiteralDecoder;
22
36
  }
23
37
  return Left(new LiteralValueError(literal, `number ${value} is not an integer`));
24
38
  }
39
+ function decodeBigDecimalLiteral(literal) {
40
+ const literalDatatypeDefinition = literalDatatypeDefinitions[literal.datatype.value];
41
+ if (!literalDatatypeDefinition) {
42
+ return Left(new LiteralDatatypeError(literal));
43
+ }
44
+ switch (literalDatatypeDefinition.kind) {
45
+ case "bigdecimal":
46
+ return decodeBigDecimalLiteralValue(literal);
47
+ case "bigint":
48
+ return decodeBigIntLiteralValue(literal).map((value) => new Decimal(value));
49
+ case "float":
50
+ return decodeFloatLiteralValue(literal).map((value) => new Decimal(value));
51
+ case "int":
52
+ return decodeIntLiteralValue(literal).map((value) => new Decimal(value));
53
+ default:
54
+ return Left(new LiteralDatatypeError(literal));
55
+ }
56
+ }
57
+ LiteralDecoder.decodeBigDecimalLiteral = decodeBigDecimalLiteral;
58
+ function decodeBigDecimalLiteralValue(literal) {
59
+ return Either.encase(() => Decimal(literal.value));
60
+ }
25
61
  function decodeBigIntLiteral(literal) {
26
62
  const literalDatatypeDefinition = literalDatatypeDefinitions[literal.datatype.value];
27
63
  if (!literalDatatypeDefinition) {
28
64
  return Left(new LiteralDatatypeError(literal));
29
65
  }
30
66
  switch (literalDatatypeDefinition.kind) {
67
+ case "bigdecimal":
68
+ return decodeBigDecimalLiteral(literal).chain((value) => convertBigDecimalToBigInt(literal, value));
31
69
  case "bigint":
32
70
  return decodeBigIntLiteralValue(literal);
33
71
  case "float":
@@ -93,6 +131,8 @@ export var LiteralDecoder;
93
131
  return Left(new LiteralDatatypeError(literal));
94
132
  }
95
133
  switch (literalDatatypeDefinition.kind) {
134
+ case "bigdecimal":
135
+ return decodeBigDecimalLiteral(literal).chain((value) => convertBigDecimalToNumber(literal, value));
96
136
  case "bigint":
97
137
  return decodeBigIntLiteral(literal).chain((value) => convertBigIntToNumber(literal, value));
98
138
  case "float":
@@ -127,6 +167,8 @@ export var LiteralDecoder;
127
167
  return Left(new LiteralDatatypeError(literal));
128
168
  }
129
169
  switch (literalDatatypeDefinition.kind) {
170
+ case "bigdecimal":
171
+ return decodeBigDecimalLiteral(literal).chain((value) => convertBigDecimalToNumber(literal, value));
130
172
  case "bigint":
131
173
  return decodeBigIntLiteral(literal).chain((value) => convertBigIntToNumber(literal, value));
132
174
  case "float":
@@ -151,6 +193,8 @@ export var LiteralDecoder;
151
193
  return Left(new LiteralDatatypeError(literal));
152
194
  }
153
195
  switch (literalDatatypeDefinition.kind) {
196
+ case "bigdecimal":
197
+ return decodeBigDecimalLiteral(literal).chain((value) => convertBigDecimalToNumber(literal, value));
154
198
  case "bigint":
155
199
  return decodeBigIntLiteralValue(literal).chain((value) => convertBigIntToNumber(literal, value));
156
200
  case "float":
@@ -1,4 +1,5 @@
1
1
  import type { DataFactory, Literal, NamedNode } from "@rdfjs/types";
2
+ import type { Decimal } from "decimal.js";
2
3
  import type { Primitive } from "./Primitive.js";
3
4
  /**
4
5
  * Factory with methods for creating RDF/JS Literals from other types.
@@ -9,12 +10,13 @@ export declare class LiteralFactory {
9
10
  constructor({ dataFactory }: {
10
11
  dataFactory: DataFactory;
11
12
  });
13
+ bigdecimal(value: Decimal, datatype?: NamedNode): Literal;
12
14
  bigint(value: bigint, datatype?: NamedNode): Literal;
13
15
  boolean(value: boolean, datatype?: NamedNode): Literal;
14
16
  date(value: Date, datatype?: NamedNode): Literal;
15
17
  number(value: number, datatype?: NamedNode): Literal;
16
- private numeric;
17
18
  primitive(value: Primitive, datatype?: NamedNode): Literal;
18
19
  string(value: string, datatype?: NamedNode): Literal;
20
+ private numeric;
19
21
  }
20
22
  //# sourceMappingURL=LiteralFactory.d.ts.map
@@ -34,6 +34,9 @@ export class LiteralFactory {
34
34
  unsignedShort: this.dataFactory.namedNode("http://www.w3.org/2001/XMLSchema#unsignedShort"),
35
35
  };
36
36
  }
37
+ bigdecimal(value, datatype) {
38
+ return this.numeric(value, datatype ?? this.xsd.decimal);
39
+ }
37
40
  bigint(value, datatype) {
38
41
  return this.numeric(value, datatype ?? this.xsd.integer);
39
42
  }
@@ -119,6 +122,27 @@ export class LiteralFactory {
119
122
  }
120
123
  return this.numeric(value, datatype);
121
124
  }
125
+ primitive(value, datatype) {
126
+ switch (typeof value) {
127
+ case "bigint":
128
+ return this.bigint(value, datatype);
129
+ case "boolean":
130
+ return this.boolean(value, datatype);
131
+ case "number":
132
+ return this.number(value, datatype);
133
+ case "string":
134
+ return this.string(value, datatype);
135
+ }
136
+ }
137
+ string(value, datatype) {
138
+ if (datatype) {
139
+ const datatypeDefinition = literalDatatypeDefinitions[datatype.value];
140
+ if (datatypeDefinition && datatypeDefinition.kind !== "string") {
141
+ throw new DatatypeRangeError(datatype);
142
+ }
143
+ }
144
+ return this.dataFactory.literal(value, datatype);
145
+ }
122
146
  numeric(value, datatype) {
123
147
  let valueString;
124
148
  const datatypeDefinition = literalDatatypeDefinitions[datatype.value];
@@ -131,13 +155,30 @@ export class LiteralFactory {
131
155
  else {
132
156
  switch (datatypeDefinition.kind) {
133
157
  case "bigdecimal":
158
+ break;
134
159
  case "bigint":
135
160
  case "float":
136
161
  case "int": {
137
162
  const [min, max] = datatypeDefinition.range;
138
- if ((min !== undefined && value < min) ||
139
- (max !== undefined && value > max)) {
140
- throw new RangeError(`value (${value}) outside range [${min}, ${max}] of ${datatype.value}`);
163
+ if (max !== undefined) {
164
+ if (typeof value === "object") {
165
+ if (value.gt(max)) {
166
+ throw new RangeError(`value (${value}) above maximum (${max}) of ${datatype.value}`);
167
+ }
168
+ }
169
+ else if (value > max) {
170
+ throw new RangeError(`value (${value}) above maximum (${max}) of ${datatype.value}`);
171
+ }
172
+ }
173
+ if (min !== undefined) {
174
+ if (typeof value === "object") {
175
+ if (value.lt(min)) {
176
+ throw new RangeError(`value (${value}) below minimum (${min}) of ${datatype.value}`);
177
+ }
178
+ }
179
+ else if (value < min) {
180
+ throw new RangeError(`value (${value}) below minimum (${min}) of ${datatype.value}`);
181
+ }
141
182
  }
142
183
  break;
143
184
  }
@@ -172,26 +213,5 @@ export class LiteralFactory {
172
213
  }
173
214
  return this.dataFactory.literal(valueString, datatype);
174
215
  }
175
- primitive(value, datatype) {
176
- switch (typeof value) {
177
- case "bigint":
178
- return this.bigint(value, datatype);
179
- case "boolean":
180
- return this.boolean(value, datatype);
181
- case "number":
182
- return this.number(value, datatype);
183
- case "string":
184
- return this.string(value, datatype);
185
- }
186
- }
187
- string(value, datatype) {
188
- if (datatype) {
189
- const datatypeDefinition = literalDatatypeDefinitions[datatype.value];
190
- if (datatypeDefinition && datatypeDefinition.kind !== "string") {
191
- throw new DatatypeRangeError(datatype);
192
- }
193
- }
194
- return this.dataFactory.literal(value, datatype);
195
- }
196
216
  }
197
217
  //# sourceMappingURL=LiteralFactory.js.map
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "dependencies": {
3
3
  "@rdfjs/types": "~2.0.1",
4
+ "decimal.js": "~10.6.0",
4
5
  "purify-ts": "~2.1.4"
5
6
  },
6
7
  "devDependencies": {
7
- "@rdfx/data-factory": "0.0.21",
8
+ "@rdfx/data-factory": "0.0.22",
8
9
  "@tpluscode/rdf-ns-builders": "~4.3.0"
9
10
  },
10
11
  "files": [
@@ -30,5 +31,5 @@
30
31
  },
31
32
  "type": "module",
32
33
  "types": "./dist/index.d.ts",
33
- "version": "0.0.21"
34
+ "version": "0.0.22"
34
35
  }