@comunica/utils-expression-evaluator 3.2.4-alpha.47.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.
Files changed (60) hide show
  1. package/LICENSE.txt +22 -0
  2. package/README.md +102 -0
  3. package/lib/expressions/Aggregate.d.ts +9 -0
  4. package/lib/expressions/Aggregate.js +13 -0
  5. package/lib/expressions/Aggregate.js.map +1 -0
  6. package/lib/expressions/Existence.d.ts +8 -0
  7. package/lib/expressions/Existence.js +12 -0
  8. package/lib/expressions/Existence.js.map +1 -0
  9. package/lib/expressions/Expressions.d.ts +2 -0
  10. package/lib/expressions/Expressions.js +11 -0
  11. package/lib/expressions/Expressions.js.map +1 -0
  12. package/lib/expressions/Operator.d.ts +9 -0
  13. package/lib/expressions/Operator.js +14 -0
  14. package/lib/expressions/Operator.js.map +1 -0
  15. package/lib/expressions/Term.d.ts +180 -0
  16. package/lib/expressions/Term.js +348 -0
  17. package/lib/expressions/Term.js.map +1 -0
  18. package/lib/expressions/Variable.d.ts +7 -0
  19. package/lib/expressions/Variable.js +12 -0
  20. package/lib/expressions/Variable.js.map +1 -0
  21. package/lib/expressions/index.d.ts +6 -0
  22. package/lib/expressions/index.js +23 -0
  23. package/lib/expressions/index.js.map +1 -0
  24. package/lib/functions/Helpers.d.ts +81 -0
  25. package/lib/functions/Helpers.js +218 -0
  26. package/lib/functions/Helpers.js.map +1 -0
  27. package/lib/functions/OverloadTree.d.ts +45 -0
  28. package/lib/functions/OverloadTree.js +196 -0
  29. package/lib/functions/OverloadTree.js.map +1 -0
  30. package/lib/index.d.ts +12 -0
  31. package/lib/index.js +99 -0
  32. package/lib/index.js.map +1 -0
  33. package/lib/transformers/TermTransformer.d.ts +24 -0
  34. package/lib/transformers/TermTransformer.js +133 -0
  35. package/lib/transformers/TermTransformer.js.map +1 -0
  36. package/lib/util/Consts.d.ts +136 -0
  37. package/lib/util/Consts.js +171 -0
  38. package/lib/util/Consts.js.map +1 -0
  39. package/lib/util/Context.d.ts +2 -0
  40. package/lib/util/Context.js +32 -0
  41. package/lib/util/Context.js.map +1 -0
  42. package/lib/util/DateTimeHelpers.d.ts +19 -0
  43. package/lib/util/DateTimeHelpers.js +166 -0
  44. package/lib/util/DateTimeHelpers.js.map +1 -0
  45. package/lib/util/Errors.d.ts +130 -0
  46. package/lib/util/Errors.js +194 -0
  47. package/lib/util/Errors.js.map +1 -0
  48. package/lib/util/Parsing.d.ts +25 -0
  49. package/lib/util/Parsing.js +166 -0
  50. package/lib/util/Parsing.js.map +1 -0
  51. package/lib/util/Serialization.d.ts +5 -0
  52. package/lib/util/Serialization.js +54 -0
  53. package/lib/util/Serialization.js.map +1 -0
  54. package/lib/util/SpecAlgos.d.ts +4 -0
  55. package/lib/util/SpecAlgos.js +81 -0
  56. package/lib/util/SpecAlgos.js.map +1 -0
  57. package/lib/util/TypeHandling.d.ts +56 -0
  58. package/lib/util/TypeHandling.js +216 -0
  59. package/lib/util/TypeHandling.js.map +1 -0
  60. package/package.json +50 -0
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+ // TODO: Find a library for this, because this is basically an xsd datatypes parser
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.parseDayTimeDuration = exports.parseYearMonthDuration = exports.parseDuration = exports.parseTime = exports.parseDate = exports.parseDateTime = exports.parseXSDDecimal = exports.parseXSDFloat = void 0;
5
+ const DateTimeHelpers_1 = require("./DateTimeHelpers");
6
+ const Errors_1 = require("./Errors");
7
+ const SpecAlgos_1 = require("./SpecAlgos");
8
+ /**
9
+ * TODO: Fix decently
10
+ * Parses float datatypes (double, float).
11
+ *
12
+ * All invalid lexical values return undefined.
13
+ *
14
+ * @param value the string to interpret as a number
15
+ */
16
+ function parseXSDFloat(value) {
17
+ const numb = Number(value);
18
+ if (Number.isNaN(numb)) {
19
+ if (value === 'NaN') {
20
+ return Number.NaN;
21
+ }
22
+ if (value === 'INF' || value === '+INF') {
23
+ return Number.POSITIVE_INFINITY;
24
+ }
25
+ if (value === '-INF') {
26
+ return Number.NEGATIVE_INFINITY;
27
+ }
28
+ return undefined;
29
+ }
30
+ return numb;
31
+ }
32
+ exports.parseXSDFloat = parseXSDFloat;
33
+ /**
34
+ * Parses decimal datatypes (decimal, int, byte, nonPositiveInteger, etc...).
35
+ *
36
+ * All other values, including NaN, INF, and floating point numbers all
37
+ * return undefined;
38
+ *
39
+ * @param value the string to interpret as a number
40
+ */
41
+ function parseXSDDecimal(value) {
42
+ const numb = Number(value);
43
+ return Number.isNaN(numb) ? undefined : numb;
44
+ }
45
+ exports.parseXSDDecimal = parseXSDDecimal;
46
+ function parseDateTime(dateTimeStr) {
47
+ // https://www.w3.org/TR/xmlschema-2/#dateTime
48
+ const [date, time] = dateTimeStr.split('T');
49
+ if (time === undefined) {
50
+ throw new Errors_1.ParseError(dateTimeStr, 'dateTime');
51
+ }
52
+ return { ...parseDate(date), ...__parseTime(time) };
53
+ }
54
+ exports.parseDateTime = parseDateTime;
55
+ function parseTimeZone(timeZoneStr) {
56
+ // https://www.w3.org/TR/xmlschema-2/#dateTime-timezones
57
+ if (timeZoneStr === '') {
58
+ return { zoneHours: undefined, zoneMinutes: undefined };
59
+ }
60
+ if (timeZoneStr === 'Z') {
61
+ return { zoneHours: 0, zoneMinutes: 0 };
62
+ }
63
+ const timeZoneStrings = timeZoneStr.replaceAll(/^([+|-])(\d\d):(\d\d)$/gu, '$11!$2!$3').split('!');
64
+ const timeZone = timeZoneStrings.map(Number);
65
+ return {
66
+ zoneHours: timeZone[0] * timeZone[1],
67
+ zoneMinutes: timeZone[0] * timeZone[2],
68
+ };
69
+ }
70
+ function parseDate(dateStr) {
71
+ // https://www.w3.org/TR/xmlschema-2/#date-lexical-representation
72
+ const formatted = dateStr.replaceAll(/^(-)?([123456789]*\d{4})-(\d\d)-(\d\d)(Z|([+-]\d\d:\d\d))?$/gu, '$11!$2!$3!$4!$5');
73
+ if (formatted === dateStr) {
74
+ throw new Errors_1.ParseError(dateStr, 'date');
75
+ }
76
+ const dateStrings = formatted.split('!');
77
+ const date = dateStrings.slice(0, -1).map(Number);
78
+ const res = {
79
+ year: date[0] * date[1],
80
+ month: date[2],
81
+ day: date[3],
82
+ ...parseTimeZone(dateStrings[4]),
83
+ };
84
+ if (!(res.month >= 1 && res.month <= 12) || !(res.day >= 1 && res.day <= (0, SpecAlgos_1.maximumDayInMonthFor)(res.year, res.month))) {
85
+ throw new Errors_1.ParseError(dateStr, 'date');
86
+ }
87
+ return res;
88
+ }
89
+ exports.parseDate = parseDate;
90
+ function __parseTime(timeStr) {
91
+ // https://www.w3.org/TR/xmlschema-2/#time-lexical-repr
92
+ const formatted = timeStr.replaceAll(/^(\d\d):(\d\d):(\d\d(\.\d+)?)(Z|([+-]\d\d:\d\d))?$/gu, '$1!$2!$3!$5');
93
+ if (formatted === timeStr) {
94
+ throw new Errors_1.ParseError(timeStr, 'time');
95
+ }
96
+ const timeStrings = formatted.split('!');
97
+ const time = timeStrings.slice(0, -1).map(Number);
98
+ const res = {
99
+ hours: time[0],
100
+ minutes: time[1],
101
+ seconds: time[2],
102
+ ...parseTimeZone(timeStrings[3]),
103
+ };
104
+ if (res.seconds >= 60 || res.minutes >= 60 || res.hours > 24 ||
105
+ (res.hours === 24 && (res.minutes !== 0 || res.seconds !== 0))) {
106
+ throw new Errors_1.ParseError(timeStr, 'time');
107
+ }
108
+ return res;
109
+ }
110
+ // We make a separation in internal and external since dateTime will have hour-date rollover,
111
+ // but time just does modulo the time.
112
+ function parseTime(timeStr) {
113
+ // https://www.w3.org/TR/xmlschema-2/#time-lexical-repr
114
+ const res = __parseTime(timeStr);
115
+ res.hours %= 24;
116
+ return res;
117
+ }
118
+ exports.parseTime = parseTime;
119
+ function parseDuration(durationStr) {
120
+ // https://www.w3.org/TR/xmlschema-2/#duration-lexical-repr
121
+ const [dayNotation, timeNotation] = durationStr.split('T');
122
+ // Handle date part
123
+ const formattedDayDur = dayNotation.replaceAll(/^(-)?P(\d+Y)?(\d+M)?(\d+D)?$/gu, '$11S!$2!$3!$4');
124
+ if (formattedDayDur === dayNotation) {
125
+ throw new Errors_1.ParseError(durationStr, 'duration');
126
+ }
127
+ const durationStrings = formattedDayDur.split('!');
128
+ if (timeNotation !== undefined) {
129
+ const formattedTimeDur = timeNotation.replaceAll(/^(\d+H)?(\d+M)?(\d+(\.\d+)?S)?$/gu, '$1!$2!$3');
130
+ if (timeNotation === '' || timeNotation === formattedTimeDur) {
131
+ throw new Errors_1.ParseError(durationStr, 'duration');
132
+ }
133
+ durationStrings.push(...formattedTimeDur.split('!'));
134
+ }
135
+ const duration = durationStrings.map(str => str.slice(0, -1));
136
+ if (!duration.slice(1).some(Boolean)) {
137
+ throw new Errors_1.ParseError(durationStr, 'duration');
138
+ }
139
+ const sign = Number(duration[0]);
140
+ return (0, DateTimeHelpers_1.simplifyDurationRepresentation)({
141
+ year: duration[1] ? sign * Number(duration[1]) : undefined,
142
+ month: duration[2] ? sign * Number(duration[2]) : undefined,
143
+ day: duration[3] ? sign * Number(duration[3]) : undefined,
144
+ hours: duration[4] ? sign * Number(duration[4]) : undefined,
145
+ minutes: duration[5] ? sign * Number(duration[5]) : undefined,
146
+ seconds: duration[6] ? sign * Number(duration[6]) : undefined,
147
+ });
148
+ }
149
+ exports.parseDuration = parseDuration;
150
+ function parseYearMonthDuration(durationStr) {
151
+ const res = parseDuration(durationStr);
152
+ if (['hours', 'minutes', 'seconds', 'day'].some(key => Boolean(res[key]))) {
153
+ throw new Errors_1.ParseError(durationStr, 'yearMonthDuration');
154
+ }
155
+ return res;
156
+ }
157
+ exports.parseYearMonthDuration = parseYearMonthDuration;
158
+ function parseDayTimeDuration(durationStr) {
159
+ const res = parseDuration(durationStr);
160
+ if (['year', 'month'].some(key => Boolean(res[key]))) {
161
+ throw new Errors_1.ParseError(durationStr, 'dayTimeDuration');
162
+ }
163
+ return res;
164
+ }
165
+ exports.parseDayTimeDuration = parseDayTimeDuration;
166
+ //# sourceMappingURL=Parsing.js.map
@@ -0,0 +1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CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS;KAC9D,CAAC,CAAC;AACL,CAAC;AAjCD,sCAiCC;AAED,SAAgB,sBAAsB,CAAC,WAAmB;IACxD,MAAM,GAAG,GAAG,aAAa,CAAC,WAAW,CAAC,CAAC;IACvC,IAAI,CAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,KAAK,CAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,OAAO,CAAQ,GAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;QACpF,MAAM,IAAI,mBAAU,CAAC,WAAW,EAAE,mBAAmB,CAAC,CAAC;IACzD,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAND,wDAMC;AAED,SAAgB,oBAAoB,CAAC,WAAmB;IACtD,MAAM,GAAG,GAAG,aAAa,CAAC,WAAW,CAAC,CAAC;IACvC,IAAI,CAAE,MAAM,EAAE,OAAO,CAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,OAAO,CAAQ,GAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;QAC/D,MAAM,IAAI,mBAAU,CAAC,WAAW,EAAE,iBAAiB,CAAC,CAAC;IACvD,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAND,oDAMC","sourcesContent":["// TODO: Find a library for this, because this is basically an xsd datatypes parser\n\nimport type {\n IDateRepresentation,\n IDateTimeRepresentation,\n IDayTimeDurationRepresentation,\n IDurationRepresentation,\n ITimeRepresentation,\n ITimeZoneRepresentation,\n IYearMonthDurationRepresentation,\n} from '@comunica/types';\nimport { simplifyDurationRepresentation } from './DateTimeHelpers';\nimport { ParseError } from './Errors';\nimport { maximumDayInMonthFor } from './SpecAlgos';\n\n/**\n * TODO: Fix decently\n * Parses float datatypes (double, float).\n *\n * All invalid lexical values return undefined.\n *\n * @param value the string to interpret as a number\n */\nexport function parseXSDFloat(value: string): number | undefined {\n const numb = Number(value);\n if (Number.isNaN(numb)) {\n if (value === 'NaN') {\n return Number.NaN;\n }\n if (value === 'INF' || value === '+INF') {\n return Number.POSITIVE_INFINITY;\n }\n if (value === '-INF') {\n return Number.NEGATIVE_INFINITY;\n }\n return undefined;\n }\n return numb;\n}\n\n/**\n * Parses decimal datatypes (decimal, int, byte, nonPositiveInteger, etc...).\n *\n * All other values, including NaN, INF, and floating point numbers all\n * return undefined;\n *\n * @param value the string to interpret as a number\n */\nexport function parseXSDDecimal(value: string): number | undefined {\n const numb = Number(value);\n return Number.isNaN(numb) ? undefined : numb;\n}\n\nexport function parseDateTime(dateTimeStr: string): IDateTimeRepresentation {\n // https://www.w3.org/TR/xmlschema-2/#dateTime\n const [ date, time ] = dateTimeStr.split('T');\n if (time === undefined) {\n throw new ParseError(dateTimeStr, 'dateTime');\n }\n return { ...parseDate(date), ...__parseTime(time) };\n}\n\nfunction parseTimeZone(timeZoneStr: string): Partial<ITimeZoneRepresentation> {\n // https://www.w3.org/TR/xmlschema-2/#dateTime-timezones\n if (timeZoneStr === '') {\n return { zoneHours: undefined, zoneMinutes: undefined };\n }\n if (timeZoneStr === 'Z') {\n return { zoneHours: 0, zoneMinutes: 0 };\n }\n const timeZoneStrings = timeZoneStr.replaceAll(/^([+|-])(\\d\\d):(\\d\\d)$/gu, '$11!$2!$3').split('!');\n const timeZone = timeZoneStrings.map(Number);\n return {\n zoneHours: timeZone[0] * timeZone[1],\n zoneMinutes: timeZone[0] * timeZone[2],\n };\n}\n\nexport function parseDate(dateStr: string): IDateRepresentation {\n // https://www.w3.org/TR/xmlschema-2/#date-lexical-representation\n const formatted = dateStr.replaceAll(\n /^(-)?([123456789]*\\d{4})-(\\d\\d)-(\\d\\d)(Z|([+-]\\d\\d:\\d\\d))?$/gu,\n '$11!$2!$3!$4!$5',\n );\n if (formatted === dateStr) {\n throw new ParseError(dateStr, 'date');\n }\n const dateStrings = formatted.split('!');\n const date = dateStrings.slice(0, -1).map(Number);\n\n const res = {\n year: date[0] * date[1],\n month: date[2],\n day: date[3],\n ...parseTimeZone(dateStrings[4]),\n };\n if (!(res.month >= 1 && res.month <= 12) || !(res.day >= 1 && res.day <= maximumDayInMonthFor(res.year, res.month))) {\n throw new ParseError(dateStr, 'date');\n }\n return res;\n}\n\nfunction __parseTime(timeStr: string): ITimeRepresentation {\n // https://www.w3.org/TR/xmlschema-2/#time-lexical-repr\n const formatted = timeStr.replaceAll(/^(\\d\\d):(\\d\\d):(\\d\\d(\\.\\d+)?)(Z|([+-]\\d\\d:\\d\\d))?$/gu, '$1!$2!$3!$5');\n if (formatted === timeStr) {\n throw new ParseError(timeStr, 'time');\n }\n const timeStrings = formatted.split('!');\n const time = timeStrings.slice(0, -1).map(Number);\n\n const res = {\n hours: time[0],\n minutes: time[1],\n seconds: time[2],\n ...parseTimeZone(timeStrings[3]),\n };\n\n if (res.seconds >= 60 || res.minutes >= 60 || res.hours > 24 ||\n (res.hours === 24 && (res.minutes !== 0 || res.seconds !== 0))) {\n throw new ParseError(timeStr, 'time');\n }\n return res;\n}\n\n// We make a separation in internal and external since dateTime will have hour-date rollover,\n// but time just does modulo the time.\nexport function parseTime(timeStr: string): ITimeRepresentation {\n // https://www.w3.org/TR/xmlschema-2/#time-lexical-repr\n const res = __parseTime(timeStr);\n res.hours %= 24;\n return res;\n}\n\nexport function parseDuration(durationStr: string): Partial<IDurationRepresentation> {\n // https://www.w3.org/TR/xmlschema-2/#duration-lexical-repr\n const [ dayNotation, timeNotation ] = durationStr.split('T');\n\n // Handle date part\n const formattedDayDur = dayNotation.replaceAll(/^(-)?P(\\d+Y)?(\\d+M)?(\\d+D)?$/gu, '$11S!$2!$3!$4');\n if (formattedDayDur === dayNotation) {\n throw new ParseError(durationStr, 'duration');\n }\n\n const durationStrings = formattedDayDur.split('!');\n if (timeNotation !== undefined) {\n const formattedTimeDur = timeNotation.replaceAll(/^(\\d+H)?(\\d+M)?(\\d+(\\.\\d+)?S)?$/gu, '$1!$2!$3');\n\n if (timeNotation === '' || timeNotation === formattedTimeDur) {\n throw new ParseError(durationStr, 'duration');\n }\n durationStrings.push(...formattedTimeDur.split('!'));\n }\n const duration = durationStrings.map(str => str.slice(0, -1));\n if (!duration.slice(1).some(Boolean)) {\n throw new ParseError(durationStr, 'duration');\n }\n\n const sign = <-1 | 1> Number(duration[0]);\n return simplifyDurationRepresentation({\n year: duration[1] ? sign * Number(duration[1]) : undefined,\n month: duration[2] ? sign * Number(duration[2]) : undefined,\n day: duration[3] ? sign * Number(duration[3]) : undefined,\n hours: duration[4] ? sign * Number(duration[4]) : undefined,\n minutes: duration[5] ? sign * Number(duration[5]) : undefined,\n seconds: duration[6] ? sign * Number(duration[6]) : undefined,\n });\n}\n\nexport function parseYearMonthDuration(durationStr: string): Partial<IYearMonthDurationRepresentation> {\n const res = parseDuration(durationStr);\n if ([ 'hours', 'minutes', 'seconds', 'day' ].some(key => Boolean((<any> res)[key]))) {\n throw new ParseError(durationStr, 'yearMonthDuration');\n }\n return res;\n}\n\nexport function parseDayTimeDuration(durationStr: string): Partial<IDayTimeDurationRepresentation> {\n const res = parseDuration(durationStr);\n if ([ 'year', 'month' ].some(key => Boolean((<any> res)[key]))) {\n throw new ParseError(durationStr, 'dayTimeDuration');\n }\n return res;\n}\n"]}
@@ -0,0 +1,5 @@
1
+ import type { IDateRepresentation, IDateTimeRepresentation, IDurationRepresentation, ITimeRepresentation } from '@comunica/types';
2
+ export declare function serializeDateTime(date: IDateTimeRepresentation): string;
3
+ export declare function serializeDate(date: IDateRepresentation): string;
4
+ export declare function serializeTime(time: ITimeRepresentation): string;
5
+ export declare function serializeDuration(dur: Partial<IDurationRepresentation>, zeroString?: 'PT0S' | 'P0M'): string;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.serializeDuration = exports.serializeTime = exports.serializeDate = exports.serializeDateTime = void 0;
4
+ function numSerializer(num, min = 2) {
5
+ return num.toLocaleString(undefined, { minimumIntegerDigits: min, useGrouping: false });
6
+ }
7
+ function serializeDateTime(date) {
8
+ // https://www.w3.org/TR/xmlschema-2/#dateTime
9
+ // Extraction is needed because the date serializer can not add timezone y
10
+ return `${serializeDate({ year: date.year, month: date.month, day: date.day })}T${serializeTime(date)}`;
11
+ }
12
+ exports.serializeDateTime = serializeDateTime;
13
+ function serializeTimeZone(tz) {
14
+ // https://www.w3.org/TR/xmlschema-2/#dateTime-timezones
15
+ if (tz.zoneHours === undefined || tz.zoneMinutes === undefined) {
16
+ return '';
17
+ }
18
+ if (tz.zoneHours === 0 && tz.zoneMinutes === 0) {
19
+ return 'Z';
20
+ }
21
+ // SerializeTimeZone({ zoneHours: 5, zoneMinutes: 4 }) returns +05:04
22
+ return `${tz.zoneHours >= 0 ? `+${numSerializer(tz.zoneHours)}` : numSerializer(tz.zoneHours)}:${numSerializer(Math.abs(tz.zoneMinutes))}`;
23
+ }
24
+ function serializeDate(date) {
25
+ // https://www.w3.org/TR/xmlschema-2/#date-lexical-representation
26
+ return `${numSerializer(date.year, 4)}-${numSerializer(date.month)}-${numSerializer(date.day)}${serializeTimeZone(date)}`;
27
+ }
28
+ exports.serializeDate = serializeDate;
29
+ function serializeTime(time) {
30
+ // https://www.w3.org/TR/xmlschema-2/#time-lexical-repr
31
+ return `${numSerializer(time.hours)}:${numSerializer(time.minutes)}:${numSerializer(time.seconds)}${serializeTimeZone(time)}`;
32
+ }
33
+ exports.serializeTime = serializeTime;
34
+ function serializeDuration(dur, zeroString = 'PT0S') {
35
+ // https://www.w3.org/TR/xmlschema-2/#duration-lexical-repr
36
+ if (!Object.values(dur).some(val => (val || 0) !== 0)) {
37
+ return zeroString;
38
+ }
39
+ const sign = Object.values(dur).some(val => (val || 0) < 0) ? '-' : '';
40
+ const year = dur.year ? `${Math.abs(dur.year)}Y` : '';
41
+ const month = dur.month ? `${Math.abs(dur.month)}M` : '';
42
+ const day = dur.day ? `${Math.abs(dur.day)}D` : '';
43
+ const dayNotation = `${sign}P${year}${month}${day}`;
44
+ // eslint-disable-next-line ts/prefer-nullish-coalescing
45
+ if (!(dur.hours || dur.minutes || dur.seconds)) {
46
+ return dayNotation;
47
+ }
48
+ const hour = dur.hours ? `${Math.abs(dur.hours)}H` : '';
49
+ const minute = dur.minutes ? `${Math.abs(dur.minutes)}M` : '';
50
+ const second = dur.seconds ? `${Math.abs(dur.seconds)}S` : '';
51
+ return `${dayNotation}T${hour}${minute}${second}`;
52
+ }
53
+ exports.serializeDuration = serializeDuration;
54
+ //# sourceMappingURL=Serialization.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Serialization.js","sourceRoot":"","sources":["Serialization.ts"],"names":[],"mappings":";;;AAQA,SAAS,aAAa,CAAC,GAAW,EAAE,GAAG,GAAG,CAAC;IACzC,OAAO,GAAG,CAAC,cAAc,CAAC,SAAS,EAAE,EAAE,oBAAoB,EAAE,GAAG,EAAE,WAAW,EAAE,KAAK,EAAE,CAAC,CAAC;AAC1F,CAAC;AAED,SAAgB,iBAAiB,CAAC,IAA6B;IAC7D,8CAA8C;IAC9C,0EAA0E;IAC1E,OAAO,GAAG,aAAa,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC;AAC1G,CAAC;AAJD,8CAIC;AAED,SAAS,iBAAiB,CAAC,EAAoC;IAC7D,wDAAwD;IACxD,IAAI,EAAE,CAAC,SAAS,KAAK,SAAS,IAAI,EAAE,CAAC,WAAW,KAAK,SAAS,EAAE,CAAC;QAC/D,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,IAAI,EAAE,CAAC,SAAS,KAAK,CAAC,IAAI,EAAE,CAAC,WAAW,KAAK,CAAC,EAAE,CAAC;QAC/C,OAAO,GAAG,CAAC;IACb,CAAC;IAC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G,MAAM,EAAE,CAAC;AACpD,CAAC;AAtBD,8CAsBC","sourcesContent":["import type {\n IDateRepresentation,\n IDateTimeRepresentation,\n IDurationRepresentation,\n ITimeRepresentation,\n ITimeZoneRepresentation,\n} from '@comunica/types';\n\nfunction numSerializer(num: number, min = 2): string {\n return num.toLocaleString(undefined, { minimumIntegerDigits: min, useGrouping: false });\n}\n\nexport function serializeDateTime(date: IDateTimeRepresentation): string {\n // https://www.w3.org/TR/xmlschema-2/#dateTime\n // Extraction is needed because the date serializer can not add timezone y\n return `${serializeDate({ year: date.year, month: date.month, day: date.day })}T${serializeTime(date)}`;\n}\n\nfunction serializeTimeZone(tz: Partial<ITimeZoneRepresentation>): string {\n // https://www.w3.org/TR/xmlschema-2/#dateTime-timezones\n if (tz.zoneHours === undefined || tz.zoneMinutes === undefined) {\n return '';\n }\n if (tz.zoneHours === 0 && tz.zoneMinutes === 0) {\n return 'Z';\n }\n // SerializeTimeZone({ zoneHours: 5, zoneMinutes: 4 }) returns +05:04\n return `${tz.zoneHours >= 0 ? `+${numSerializer(tz.zoneHours)}` : numSerializer(tz.zoneHours)}:${numSerializer(Math.abs(tz.zoneMinutes))}`;\n}\n\nexport function serializeDate(date: IDateRepresentation): string {\n // https://www.w3.org/TR/xmlschema-2/#date-lexical-representation\n return `${numSerializer(date.year, 4)}-${numSerializer(date.month)}-${numSerializer(date.day)}${serializeTimeZone(date)}`;\n}\n\nexport function serializeTime(time: ITimeRepresentation): string {\n // https://www.w3.org/TR/xmlschema-2/#time-lexical-repr\n return `${numSerializer(time.hours)}:${numSerializer(time.minutes)}:${numSerializer(time.seconds)}${serializeTimeZone(time)}`;\n}\n\nexport function serializeDuration(dur: Partial<IDurationRepresentation>, zeroString: 'PT0S' | 'P0M' = 'PT0S'): string {\n // https://www.w3.org/TR/xmlschema-2/#duration-lexical-repr\n if (!Object.values(dur).some(val => (val || 0) !== 0)) {\n return zeroString;\n }\n\n const sign = Object.values(dur).some(val => (val || 0) < 0) ? '-' : '';\n const year = dur.year ? `${Math.abs(dur.year)}Y` : '';\n const month = dur.month ? `${Math.abs(dur.month)}M` : '';\n const day = dur.day ? `${Math.abs(dur.day)}D` : '';\n\n const dayNotation = `${sign}P${year}${month}${day}`;\n // eslint-disable-next-line ts/prefer-nullish-coalescing\n if (!(dur.hours || dur.minutes || dur.seconds)) {\n return dayNotation;\n }\n\n const hour = dur.hours ? `${Math.abs(dur.hours)}H` : '';\n const minute = dur.minutes ? `${Math.abs(dur.minutes)}M` : '';\n const second = dur.seconds ? `${Math.abs(dur.seconds)}S` : '';\n\n return `${dayNotation}T${hour}${minute}${second}`;\n}\n"]}
@@ -0,0 +1,4 @@
1
+ import type { IDateTimeRepresentation, IDurationRepresentation, ITimeZoneRepresentation } from '@comunica/types';
2
+ export declare function maximumDayInMonthFor(yearValue: number, monthValue: number): number;
3
+ export declare function addDurationToDateTime(date: IDateTimeRepresentation, duration: IDurationRepresentation): IDateTimeRepresentation;
4
+ export declare function elapsedDuration(first: IDateTimeRepresentation, second: IDateTimeRepresentation, defaultTimeZone: ITimeZoneRepresentation): Partial<IDurationRepresentation>;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.elapsedDuration = exports.addDurationToDateTime = exports.maximumDayInMonthFor = void 0;
4
+ const DateTimeHelpers_1 = require("./DateTimeHelpers");
5
+ function fDiv(arg, high, low = 0) {
6
+ // Adds the 4 spec functions into one since they are highly related,
7
+ // and fQuotient and modulo are almost always called in pairs.
8
+ const first = arg - low;
9
+ const second = high - low;
10
+ const intDiv = Math.floor(first / second);
11
+ return { intDiv, remainder: arg - intDiv * second };
12
+ }
13
+ function maximumDayInMonthFor(yearValue, monthValue) {
14
+ const { intDiv: additionalYears, remainder: month } = fDiv(monthValue, 13, 1);
15
+ const year = yearValue + additionalYears;
16
+ if ([1, 3, 5, 7, 8, 10, 12].includes(month)) {
17
+ return 31;
18
+ }
19
+ if ([4, 6, 9, 11].includes(month)) {
20
+ return 30;
21
+ }
22
+ if (month === 2 && (fDiv(year, 400).remainder === 0 ||
23
+ (fDiv(year, 100).remainder !== 0 && fDiv(year, 4).remainder === 0))) {
24
+ return 29;
25
+ }
26
+ return 28;
27
+ }
28
+ exports.maximumDayInMonthFor = maximumDayInMonthFor;
29
+ // https://www.w3.org/TR/xmlschema-2/#adding-durations-to-dateTimes
30
+ function addDurationToDateTime(date, duration) {
31
+ // Used to cary over optional fields like timezone
32
+ const newDate = { ...date };
33
+ // Month
34
+ let tempDiv = fDiv(date.month + duration.month, 13, 1);
35
+ newDate.month = tempDiv.remainder;
36
+ // Year
37
+ newDate.year = date.year + duration.year + tempDiv.intDiv;
38
+ // Seconds
39
+ tempDiv = fDiv(date.seconds + duration.seconds, 60);
40
+ newDate.seconds = tempDiv.remainder;
41
+ // Minutes
42
+ tempDiv = fDiv(date.minutes + duration.minutes + tempDiv.intDiv, 60);
43
+ newDate.minutes = tempDiv.remainder;
44
+ // Hours
45
+ tempDiv = fDiv(date.hours + duration.hours + tempDiv.intDiv, 24);
46
+ newDate.hours = tempDiv.remainder;
47
+ // We skip a part of the spec code since: Defined spec code can not happen since it would be an invalid literal
48
+ newDate.day = date.day + duration.day + tempDiv.intDiv;
49
+ while (true) {
50
+ let carry;
51
+ if (newDate.day < 1) {
52
+ newDate.day += maximumDayInMonthFor(newDate.year, newDate.month - 1);
53
+ carry = -1;
54
+ }
55
+ else if (newDate.day > maximumDayInMonthFor(newDate.year, newDate.month)) {
56
+ newDate.day -= maximumDayInMonthFor(newDate.year, newDate.month);
57
+ carry = 1;
58
+ }
59
+ else {
60
+ break;
61
+ }
62
+ tempDiv = fDiv(newDate.month + carry, 13, 1);
63
+ newDate.month = tempDiv.remainder;
64
+ newDate.year += tempDiv.intDiv;
65
+ }
66
+ return newDate;
67
+ }
68
+ exports.addDurationToDateTime = addDurationToDateTime;
69
+ function elapsedDuration(first, second, defaultTimeZone) {
70
+ const d1 = (0, DateTimeHelpers_1.toUTCDate)(first, defaultTimeZone);
71
+ const d2 = (0, DateTimeHelpers_1.toUTCDate)(second, defaultTimeZone);
72
+ const diff = d1.getTime() - d2.getTime();
73
+ return {
74
+ day: Math.floor(diff / (1_000 * 60 * 60 * 24)),
75
+ hours: Math.floor((diff % (1_000 * 60 * 60 * 24)) / (1_000 * 60 * 60)),
76
+ minutes: Math.floor(diff % (1_000 * 60 * 60) / (1_000 * 60)),
77
+ seconds: diff % (1_000 * 60),
78
+ };
79
+ }
80
+ exports.elapsedDuration = elapsedDuration;
81
+ //# sourceMappingURL=SpecAlgos.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SpecAlgos.js","sourceRoot":"","sources":["SpecAlgos.ts"],"names":[],"mappings":";;;AACA,uDAA8C;AAE9C,SAAS,IAAI,CAAC,GAAW,EAAE,IAAY,EAAE,GAAG,GAAG,CAAC;IAC9C,oEAAoE;IACpE,8DAA8D;IAC9D,MAAM,KAAK,GAAG,GAAG,GAAG,GAAG,CAAC;IACxB,MAAM,MAAM,GAAG,IAAI,GAAG,GAAG,CAAC;IAC1B,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC,CAAC;IAC1C,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC;AACtD,CAAC;AAED,SAAgB,oBAAoB,CAAC,SAAiB,EAAE,UAAkB;IACxE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,UAAU,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;IAC9E,MAAM,IAAI,GAAG,SAAS,GAAG,eAAe,CAAC;IAEzC,IAAI,CAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,CAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QAC9C,OAAO,EAAE,CAAC;IACZ,C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C;IAExC,MAAM,EAAE,GAAG,IAAA,2BAAS,EAAC,KAAK,EAAE,eAAe,CAAC,CAAC;IAC7C,MAAM,EAAE,GAAG,IAAA,2BAAS,EAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IAC9C,MAAM,IAAI,GAAG,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC;IACzC,OAAO;QACL,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,CAAC,KAAK,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC;QAC9C,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,GAAG,CAAC,KAAK,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC;QACtE,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,CAAC,KAAK,GAAG,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,KAAK,GAAG,EAAE,CAAC,CAAC;QAC5D,OAAO,EAAE,IAAI,GAAG,CAAC,KAAK,GAAG,EAAE,CAAC;KAC7B,CAAC;AACJ,CAAC;AAdD,0CAcC","sourcesContent":["import type { IDateTimeRepresentation, IDurationRepresentation, ITimeZoneRepresentation } from '@comunica/types';\nimport { toUTCDate } from './DateTimeHelpers';\n\nfunction fDiv(arg: number, high: number, low = 0): { intDiv: number; remainder: number } {\n // Adds the 4 spec functions into one since they are highly related,\n // and fQuotient and modulo are almost always called in pairs.\n const first = arg - low;\n const second = high - low;\n const intDiv = Math.floor(first / second);\n return { intDiv, remainder: arg - intDiv * second };\n}\n\nexport function maximumDayInMonthFor(yearValue: number, monthValue: number): number {\n const { intDiv: additionalYears, remainder: month } = fDiv(monthValue, 13, 1);\n const year = yearValue + additionalYears;\n\n if ([ 1, 3, 5, 7, 8, 10, 12 ].includes(month)) {\n return 31;\n }\n if ([ 4, 6, 9, 11 ].includes(month)) {\n return 30;\n }\n if (month === 2 && (\n fDiv(year, 400).remainder === 0 ||\n (fDiv(year, 100).remainder !== 0 && fDiv(year, 4).remainder === 0))) {\n return 29;\n }\n return 28;\n}\n\n// https://www.w3.org/TR/xmlschema-2/#adding-durations-to-dateTimes\nexport function addDurationToDateTime(date: IDateTimeRepresentation, duration: IDurationRepresentation):\nIDateTimeRepresentation {\n // Used to cary over optional fields like timezone\n const newDate: IDateTimeRepresentation = { ...date };\n\n // Month\n let tempDiv = fDiv(date.month + duration.month, 13, 1);\n newDate.month = tempDiv.remainder;\n // Year\n newDate.year = date.year + duration.year + tempDiv.intDiv;\n // Seconds\n tempDiv = fDiv(date.seconds + duration.seconds, 60);\n newDate.seconds = tempDiv.remainder;\n // Minutes\n tempDiv = fDiv(date.minutes + duration.minutes + tempDiv.intDiv, 60);\n newDate.minutes = tempDiv.remainder;\n // Hours\n tempDiv = fDiv(date.hours + duration.hours + tempDiv.intDiv, 24);\n newDate.hours = tempDiv.remainder;\n\n // We skip a part of the spec code since: Defined spec code can not happen since it would be an invalid literal\n\n newDate.day = date.day + duration.day + tempDiv.intDiv;\n\n while (true) {\n let carry;\n if (newDate.day < 1) {\n newDate.day += maximumDayInMonthFor(newDate.year, newDate.month - 1);\n carry = -1;\n } else if (newDate.day > maximumDayInMonthFor(newDate.year, newDate.month)) {\n newDate.day -= maximumDayInMonthFor(newDate.year, newDate.month);\n carry = 1;\n } else {\n break;\n }\n tempDiv = fDiv(newDate.month + carry, 13, 1);\n newDate.month = tempDiv.remainder;\n newDate.year += tempDiv.intDiv;\n }\n return newDate;\n}\n\nexport function elapsedDuration(\n first: IDateTimeRepresentation,\n second: IDateTimeRepresentation,\n defaultTimeZone: ITimeZoneRepresentation,\n): Partial<IDurationRepresentation> {\n const d1 = toUTCDate(first, defaultTimeZone);\n const d2 = toUTCDate(second, defaultTimeZone);\n const diff = d1.getTime() - d2.getTime();\n return {\n day: Math.floor(diff / (1_000 * 60 * 60 * 24)),\n hours: Math.floor((diff % (1_000 * 60 * 60 * 24)) / (1_000 * 60 * 60)),\n minutes: Math.floor(diff % (1_000 * 60 * 60) / (1_000 * 60)),\n seconds: diff % (1_000 * 60),\n };\n}\n"]}
@@ -0,0 +1,56 @@
1
+ import type { GeneralSuperTypeDict, ISuperTypeProvider, TermExpression, TermType } from '@comunica/types';
2
+ import type { ArgumentType } from '../functions/OverloadTree';
3
+ import type { KnownLiteralTypes } from './Consts';
4
+ import { TypeAlias } from './Consts';
5
+ export type OverrideType = KnownLiteralTypes | 'term';
6
+ /**
7
+ * Types that are not mentioned just map to 'term'.
8
+ * When editing this, make sure type promotion and substitution don't start interfering.
9
+ * e.g. when saying something like string -> stringly -> anyUri -> term.
10
+ * This would make substitution on types that promote to each other possible. We and the specs don't want that!
11
+ * A DAG will be created based on this. Make sure it doesn't have any cycles!
12
+ */
13
+ export declare const extensionTableInput: Record<KnownLiteralTypes, OverrideType>;
14
+ type SuperTypeDict = Record<KnownLiteralTypes, number> & {
15
+ __depth: number;
16
+ };
17
+ type SuperTypeDictTable = Record<KnownLiteralTypes, SuperTypeDict>;
18
+ export declare const superTypeDictTable: SuperTypeDictTable;
19
+ /**
20
+ * This will return the super types of a type and cache them.
21
+ * @param type IRI we will decide the super types of.
22
+ * @param openWorldType the enabler that provides a way to find super types.
23
+ */
24
+ export declare function getSuperTypes(type: string, openWorldType: ISuperTypeProvider): GeneralSuperTypeDict;
25
+ export declare function extensionTableInit(): void;
26
+ export declare const typeAliasCheck: Record<TypeAlias, boolean>;
27
+ export declare function asTypeAlias(type: string): TypeAlias | undefined;
28
+ export declare function asKnownLiteralType(type: string): KnownLiteralTypes | undefined;
29
+ export declare function asOverrideType(type: string): OverrideType | undefined;
30
+ export declare function asGeneralType(type: string): 'term' | TermType | undefined;
31
+ /**
32
+ * Internal type of @see isSubTypeOf This only takes knownTypes but doesn't need an enabler
33
+ */
34
+ export declare function isInternalSubType(baseType: OverrideType, argumentType: KnownLiteralTypes): boolean;
35
+ /**
36
+ * This function can be used to check the base type is a restriction on a type in the dict.
37
+ * If we want to check if type x is a restriction on string we do this by calling:
38
+ * 'http://www.w3.org/2001/XMLSchema#string' in getSuperTypeDict(X, superTypeProvider)
39
+ * @param baseType
40
+ * @param superTypeProvider
41
+ */
42
+ export declare function getSuperTypeDict(baseType: string, superTypeProvider: ISuperTypeProvider): GeneralSuperTypeDict;
43
+ /**
44
+ * This function needs to be O(1)! The execution time of this function is vital!
45
+ * We define typeA isSubtypeOf typeA as true.
46
+ * If you find yourself using this function a lot (e.g. in a case) please use getSuperTypeDict instead.
47
+ * @param baseType type you want to provide.
48
+ * @param argumentType type you want to provide @param baseType to.
49
+ * @param superTypeProvider the enabler to discover super types of unknown types.
50
+ */
51
+ export declare function isSubTypeOf(baseType: string, argumentType: KnownLiteralTypes, superTypeProvider: ISuperTypeProvider): boolean;
52
+ export declare const typePromotion: Partial<Record<ArgumentType, {
53
+ typeToPromote: KnownLiteralTypes;
54
+ conversionFunction: (arg: TermExpression) => TermExpression;
55
+ }[]>>;
56
+ export {};
@@ -0,0 +1,216 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.typePromotion = exports.isSubTypeOf = exports.getSuperTypeDict = exports.isInternalSubType = exports.asGeneralType = exports.asOverrideType = exports.asKnownLiteralType = exports.asTypeAlias = exports.typeAliasCheck = exports.extensionTableInit = exports.getSuperTypes = exports.superTypeDictTable = exports.extensionTableInput = void 0;
4
+ const expressions_1 = require("../expressions");
5
+ const Helpers_1 = require("../functions/Helpers");
6
+ const Consts_1 = require("./Consts");
7
+ /**
8
+ * Types that are not mentioned just map to 'term'.
9
+ * When editing this, make sure type promotion and substitution don't start interfering.
10
+ * e.g. when saying something like string -> stringly -> anyUri -> term.
11
+ * This would make substitution on types that promote to each other possible. We and the specs don't want that!
12
+ * A DAG will be created based on this. Make sure it doesn't have any cycles!
13
+ */
14
+ exports.extensionTableInput = {
15
+ // Datetime types
16
+ [Consts_1.TypeURL.XSD_DATE_TIME_STAMP]: Consts_1.TypeURL.XSD_DATE_TIME,
17
+ // Duration types
18
+ [Consts_1.TypeURL.XSD_DAY_TIME_DURATION]: Consts_1.TypeURL.XSD_DURATION,
19
+ [Consts_1.TypeURL.XSD_YEAR_MONTH_DURATION]: Consts_1.TypeURL.XSD_DURATION,
20
+ // Stringly types
21
+ [Consts_1.TypeURL.RDF_LANG_STRING]: Consts_1.TypeAlias.SPARQL_STRINGLY,
22
+ [Consts_1.TypeURL.XSD_STRING]: Consts_1.TypeAlias.SPARQL_STRINGLY,
23
+ // String types
24
+ [Consts_1.TypeURL.XSD_NORMALIZED_STRING]: Consts_1.TypeURL.XSD_STRING,
25
+ [Consts_1.TypeURL.XSD_TOKEN]: Consts_1.TypeURL.XSD_NORMALIZED_STRING,
26
+ [Consts_1.TypeURL.XSD_LANGUAGE]: Consts_1.TypeURL.XSD_TOKEN,
27
+ [Consts_1.TypeURL.XSD_NM_TOKEN]: Consts_1.TypeURL.XSD_TOKEN,
28
+ [Consts_1.TypeURL.XSD_NAME]: Consts_1.TypeURL.XSD_TOKEN,
29
+ [Consts_1.TypeURL.XSD_NC_NAME]: Consts_1.TypeURL.XSD_NAME,
30
+ [Consts_1.TypeURL.XSD_ENTITY]: Consts_1.TypeURL.XSD_NC_NAME,
31
+ [Consts_1.TypeURL.XSD_ID]: Consts_1.TypeURL.XSD_NC_NAME,
32
+ [Consts_1.TypeURL.XSD_ID_REF]: Consts_1.TypeURL.XSD_NC_NAME,
33
+ // Numeric types
34
+ // https://www.w3.org/TR/sparql11-query/#operandDataTypes
35
+ // > numeric denotes typed literals with datatypes xsd:integer, xsd:decimal, xsd:float, and xsd:double
36
+ [Consts_1.TypeURL.XSD_DOUBLE]: Consts_1.TypeAlias.SPARQL_NUMERIC,
37
+ [Consts_1.TypeURL.XSD_FLOAT]: Consts_1.TypeAlias.SPARQL_NUMERIC,
38
+ [Consts_1.TypeURL.XSD_DECIMAL]: Consts_1.TypeAlias.SPARQL_NUMERIC,
39
+ // Decimal types
40
+ [Consts_1.TypeURL.XSD_INTEGER]: Consts_1.TypeURL.XSD_DECIMAL,
41
+ [Consts_1.TypeURL.XSD_NON_POSITIVE_INTEGER]: Consts_1.TypeURL.XSD_INTEGER,
42
+ [Consts_1.TypeURL.XSD_NEGATIVE_INTEGER]: Consts_1.TypeURL.XSD_NON_POSITIVE_INTEGER,
43
+ [Consts_1.TypeURL.XSD_LONG]: Consts_1.TypeURL.XSD_INTEGER,
44
+ [Consts_1.TypeURL.XSD_INT]: Consts_1.TypeURL.XSD_LONG,
45
+ [Consts_1.TypeURL.XSD_SHORT]: Consts_1.TypeURL.XSD_INT,
46
+ [Consts_1.TypeURL.XSD_BYTE]: Consts_1.TypeURL.XSD_SHORT,
47
+ [Consts_1.TypeURL.XSD_NON_NEGATIVE_INTEGER]: Consts_1.TypeURL.XSD_INTEGER,
48
+ [Consts_1.TypeURL.XSD_POSITIVE_INTEGER]: Consts_1.TypeURL.XSD_NON_NEGATIVE_INTEGER,
49
+ [Consts_1.TypeURL.XSD_UNSIGNED_LONG]: Consts_1.TypeURL.XSD_NON_NEGATIVE_INTEGER,
50
+ [Consts_1.TypeURL.XSD_UNSIGNED_INT]: Consts_1.TypeURL.XSD_UNSIGNED_LONG,
51
+ [Consts_1.TypeURL.XSD_UNSIGNED_SHORT]: Consts_1.TypeURL.XSD_UNSIGNED_INT,
52
+ [Consts_1.TypeURL.XSD_UNSIGNED_BYTE]: Consts_1.TypeURL.XSD_UNSIGNED_SHORT,
53
+ [Consts_1.TypeURL.XSD_DATE_TIME]: 'term',
54
+ [Consts_1.TypeURL.XSD_BOOLEAN]: 'term',
55
+ [Consts_1.TypeURL.XSD_DATE]: 'term',
56
+ [Consts_1.TypeURL.XSD_G_MONTH]: 'term',
57
+ [Consts_1.TypeURL.XSD_G_MONTHDAY]: 'term',
58
+ [Consts_1.TypeURL.XSD_G_YEAR]: 'term',
59
+ [Consts_1.TypeURL.XSD_G_YEAR_MONTH]: 'term',
60
+ [Consts_1.TypeURL.XSD_TIME]: 'term',
61
+ [Consts_1.TypeURL.XSD_G_DAY]: 'term',
62
+ [Consts_1.TypeURL.XSD_DURATION]: 'term',
63
+ [Consts_1.TypeAlias.SPARQL_NUMERIC]: 'term',
64
+ [Consts_1.TypeAlias.SPARQL_STRINGLY]: 'term',
65
+ [Consts_1.TypeURL.XSD_ANY_URI]: 'term',
66
+ };
67
+ exports.superTypeDictTable = Object.create(null);
68
+ /**
69
+ * This will return the super types of a type and cache them.
70
+ * @param type IRI we will decide the super types of.
71
+ * @param openWorldType the enabler that provides a way to find super types.
72
+ */
73
+ function getSuperTypes(type, openWorldType) {
74
+ const cached = openWorldType.cache.get(type);
75
+ if (cached) {
76
+ return cached;
77
+ }
78
+ const value = openWorldType.discoverer(type);
79
+ if (value === 'term') {
80
+ const res = Object.create(null);
81
+ res.__depth = 0;
82
+ res[type] = 0;
83
+ openWorldType.cache.set(type, res);
84
+ return res;
85
+ }
86
+ let subExtension;
87
+ const knownValue = asKnownLiteralType(value);
88
+ if (knownValue) {
89
+ subExtension = { ...exports.superTypeDictTable[knownValue] };
90
+ }
91
+ else {
92
+ subExtension = { ...getSuperTypes(value, openWorldType) };
93
+ }
94
+ subExtension.__depth++;
95
+ subExtension[type] = subExtension.__depth;
96
+ openWorldType.cache.set(type, subExtension);
97
+ return subExtension;
98
+ }
99
+ exports.getSuperTypes = getSuperTypes;
100
+ // No circular structure allowed! & No other keys allowed!
101
+ function extensionTableInit() {
102
+ for (const [_key, value] of Object.entries(exports.extensionTableInput)) {
103
+ const key = _key;
104
+ if (exports.superTypeDictTable[key]) {
105
+ continue;
106
+ }
107
+ extensionTableBuilderInitKey(key, value, exports.superTypeDictTable);
108
+ }
109
+ }
110
+ exports.extensionTableInit = extensionTableInit;
111
+ extensionTableInit();
112
+ function extensionTableBuilderInitKey(key, value, res) {
113
+ if (value === 'term' || value === undefined) {
114
+ const baseRes = Object.create(null);
115
+ baseRes.__depth = 0;
116
+ baseRes[key] = 0;
117
+ res[key] = baseRes;
118
+ return;
119
+ }
120
+ if (!res[value]) {
121
+ extensionTableBuilderInitKey(value, exports.extensionTableInput[value], res);
122
+ }
123
+ res[key] = { ...res[value], [key]: res[value].__depth + 1, __depth: res[value].__depth + 1 };
124
+ }
125
+ exports.typeAliasCheck = Object.create(null);
126
+ function initTypeAliasCheck() {
127
+ for (const val of Object.values(Consts_1.TypeAlias)) {
128
+ exports.typeAliasCheck[val] = true;
129
+ }
130
+ }
131
+ initTypeAliasCheck();
132
+ function asTypeAlias(type) {
133
+ if (type in exports.typeAliasCheck) {
134
+ return type;
135
+ }
136
+ return undefined;
137
+ }
138
+ exports.asTypeAlias = asTypeAlias;
139
+ function asKnownLiteralType(type) {
140
+ if (type in exports.superTypeDictTable) {
141
+ return type;
142
+ }
143
+ return undefined;
144
+ }
145
+ exports.asKnownLiteralType = asKnownLiteralType;
146
+ function asOverrideType(type) {
147
+ if (asKnownLiteralType(type) ?? type === 'term') {
148
+ return type;
149
+ }
150
+ return undefined;
151
+ }
152
+ exports.asOverrideType = asOverrideType;
153
+ function asGeneralType(type) {
154
+ if (type === 'term' || (0, expressions_1.asTermType)(type)) {
155
+ return type;
156
+ }
157
+ return undefined;
158
+ }
159
+ exports.asGeneralType = asGeneralType;
160
+ /**
161
+ * Internal type of @see isSubTypeOf This only takes knownTypes but doesn't need an enabler
162
+ */
163
+ function isInternalSubType(baseType, argumentType) {
164
+ return baseType !== 'term' &&
165
+ (exports.superTypeDictTable[baseType] && exports.superTypeDictTable[baseType][argumentType] !== undefined);
166
+ }
167
+ exports.isInternalSubType = isInternalSubType;
168
+ /**
169
+ * This function can be used to check the base type is a restriction on a type in the dict.
170
+ * If we want to check if type x is a restriction on string we do this by calling:
171
+ * 'http://www.w3.org/2001/XMLSchema#string' in getSuperTypeDict(X, superTypeProvider)
172
+ * @param baseType
173
+ * @param superTypeProvider
174
+ */
175
+ function getSuperTypeDict(baseType, superTypeProvider) {
176
+ const concreteType = asKnownLiteralType(baseType);
177
+ if (concreteType) {
178
+ // Concrete dataType is known by utils-expression-evaluator.
179
+ return exports.superTypeDictTable[concreteType];
180
+ }
181
+ // Datatype is a custom datatype
182
+ return getSuperTypes(baseType, superTypeProvider);
183
+ }
184
+ exports.getSuperTypeDict = getSuperTypeDict;
185
+ /**
186
+ * This function needs to be O(1)! The execution time of this function is vital!
187
+ * We define typeA isSubtypeOf typeA as true.
188
+ * If you find yourself using this function a lot (e.g. in a case) please use getSuperTypeDict instead.
189
+ * @param baseType type you want to provide.
190
+ * @param argumentType type you want to provide @param baseType to.
191
+ * @param superTypeProvider the enabler to discover super types of unknown types.
192
+ */
193
+ function isSubTypeOf(baseType, argumentType, superTypeProvider) {
194
+ if (baseType === 'term') {
195
+ return false;
196
+ }
197
+ return getSuperTypeDict(baseType, superTypeProvider)[argumentType] !== undefined;
198
+ }
199
+ exports.isSubTypeOf = isSubTypeOf;
200
+ // Defined by https://www.w3.org/TR/xpath-31/#promotion .
201
+ // e.g. When a function takes a string, it can also accept a XSD_ANY_URI if it's cast first.
202
+ exports.typePromotion = {
203
+ [Consts_1.TypeURL.XSD_STRING]: [
204
+ { typeToPromote: Consts_1.TypeURL.XSD_ANY_URI, conversionFunction: arg => (0, Helpers_1.string)(arg.str()) },
205
+ ],
206
+ [Consts_1.TypeURL.XSD_DOUBLE]: [
207
+ { typeToPromote: Consts_1.TypeURL.XSD_FLOAT, conversionFunction: arg => (0, Helpers_1.double)(arg.typedValue) },
208
+ // TODO: in case of decimal a round needs to happen.
209
+ { typeToPromote: Consts_1.TypeURL.XSD_DECIMAL, conversionFunction: arg => (0, Helpers_1.double)(arg.typedValue) },
210
+ ],
211
+ [Consts_1.TypeURL.XSD_FLOAT]: [
212
+ // TODO: in case of decimal a round needs to happen.
213
+ { typeToPromote: Consts_1.TypeURL.XSD_DECIMAL, conversionFunction: arg => (0, Helpers_1.float)(arg.typedValue) },
214
+ ],
215
+ };
216
+ //# sourceMappingURL=TypeHandling.js.map