@mapwhit/tilerenderer 0.50.0 → 0.51.1

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 (187) hide show
  1. package/build/min/package.json +1 -1
  2. package/build/min/src/shaders/.dir +0 -0
  3. package/build/min/src/shaders/{_prelude.fragment.glsl.txt → _prelude.fragment.glsl.js} +2 -1
  4. package/build/min/src/shaders/{_prelude.vertex.glsl.txt → _prelude.vertex.glsl.js} +2 -2
  5. package/build/min/src/shaders/background.fragment.glsl.js +5 -0
  6. package/build/min/src/shaders/background.vertex.glsl.js +1 -0
  7. package/build/min/src/shaders/background_pattern.fragment.glsl.js +5 -0
  8. package/build/min/src/shaders/background_pattern.vertex.glsl.js +1 -0
  9. package/build/min/src/shaders/{circle.fragment.glsl.txt → circle.fragment.glsl.js} +2 -2
  10. package/build/min/src/shaders/{circle.vertex.glsl.txt → circle.vertex.glsl.js} +2 -2
  11. package/build/min/src/shaders/clipping_mask.fragment.glsl.js +1 -0
  12. package/build/min/src/shaders/clipping_mask.vertex.glsl.js +1 -0
  13. package/build/min/src/shaders/collision_box.fragment.glsl.js +1 -0
  14. package/build/min/src/shaders/collision_box.vertex.glsl.js +1 -0
  15. package/build/min/src/shaders/collision_circle.fragment.glsl.js +1 -0
  16. package/build/min/src/shaders/collision_circle.vertex.glsl.js +1 -0
  17. package/build/min/src/shaders/debug.fragment.glsl.js +1 -0
  18. package/build/min/src/shaders/debug.vertex.glsl.js +1 -0
  19. package/build/min/src/shaders/{fill.fragment.glsl.txt → fill.fragment.glsl.js} +2 -2
  20. package/build/min/src/shaders/{fill.vertex.glsl.txt → fill.vertex.glsl.js} +2 -2
  21. package/build/min/src/shaders/fill_extrusion.fragment.glsl.js +5 -0
  22. package/build/min/src/shaders/{fill_extrusion.vertex.glsl.txt → fill_extrusion.vertex.glsl.js} +2 -2
  23. package/build/min/src/shaders/{fill_extrusion_pattern.fragment.glsl.txt → fill_extrusion_pattern.fragment.glsl.js} +2 -2
  24. package/build/min/src/shaders/{fill_extrusion_pattern.vertex.glsl.txt → fill_extrusion_pattern.vertex.glsl.js} +2 -2
  25. package/build/min/src/shaders/{fill_outline.fragment.glsl.txt → fill_outline.fragment.glsl.js} +2 -2
  26. package/build/min/src/shaders/{fill_outline.vertex.glsl.txt → fill_outline.vertex.glsl.js} +2 -2
  27. package/build/min/src/shaders/{fill_outline_pattern.fragment.glsl.txt → fill_outline_pattern.fragment.glsl.js} +2 -2
  28. package/build/min/src/shaders/{fill_outline_pattern.vertex.glsl.txt → fill_outline_pattern.vertex.glsl.js} +2 -2
  29. package/build/min/src/shaders/{fill_pattern.fragment.glsl.txt → fill_pattern.fragment.glsl.js} +2 -2
  30. package/build/min/src/shaders/{fill_pattern.vertex.glsl.txt → fill_pattern.vertex.glsl.js} +2 -2
  31. package/build/min/src/shaders/{heatmap.fragment.glsl.txt → heatmap.fragment.glsl.js} +2 -2
  32. package/build/min/src/shaders/{heatmap.vertex.glsl.txt → heatmap.vertex.glsl.js} +2 -2
  33. package/build/min/src/shaders/heatmap_texture.fragment.glsl.js +5 -0
  34. package/build/min/src/shaders/heatmap_texture.vertex.glsl.js +1 -0
  35. package/build/min/src/shaders/{hillshade.fragment.glsl.txt → hillshade.fragment.glsl.js} +2 -2
  36. package/build/min/src/shaders/hillshade.vertex.glsl.js +1 -0
  37. package/build/min/src/shaders/{hillshade_prepare.fragment.glsl.txt → hillshade_prepare.fragment.glsl.js} +2 -2
  38. package/build/min/src/shaders/hillshade_prepare.vertex.glsl.js +1 -0
  39. package/build/min/src/shaders/{line.fragment.glsl.txt → line.fragment.glsl.js} +2 -2
  40. package/build/min/src/shaders/{line.vertex.glsl.txt → line.vertex.glsl.js} +2 -2
  41. package/build/min/src/shaders/{line_gradient.fragment.glsl.txt → line_gradient.fragment.glsl.js} +2 -2
  42. package/build/min/src/shaders/{line_gradient.vertex.glsl.txt → line_gradient.vertex.glsl.js} +2 -2
  43. package/build/min/src/shaders/{line_pattern.fragment.glsl.txt → line_pattern.fragment.glsl.js} +2 -2
  44. package/build/min/src/shaders/{line_pattern.vertex.glsl.txt → line_pattern.vertex.glsl.js} +2 -2
  45. package/build/min/src/shaders/{line_sdf.fragment.glsl.txt → line_sdf.fragment.glsl.js} +2 -2
  46. package/build/min/src/shaders/{line_sdf.vertex.glsl.txt → line_sdf.vertex.glsl.js} +2 -2
  47. package/build/min/src/shaders/raster.fragment.glsl.js +5 -0
  48. package/build/min/src/shaders/raster.vertex.glsl.js +1 -0
  49. package/build/min/src/shaders/{symbol_icon.fragment.glsl.txt → symbol_icon.fragment.glsl.js} +2 -2
  50. package/build/min/src/shaders/{symbol_icon.vertex.glsl.txt → symbol_icon.vertex.glsl.js} +2 -2
  51. package/build/min/src/shaders/{symbol_sdf.fragment.glsl.txt → symbol_sdf.fragment.glsl.js} +2 -2
  52. package/build/min/src/shaders/{symbol_sdf.vertex.glsl.txt → symbol_sdf.vertex.glsl.js} +2 -2
  53. package/package.json +14 -10
  54. package/src/data/array_types.js +1 -1
  55. package/src/data/bucket/fill_bucket.js +1 -1
  56. package/src/data/bucket/fill_extrusion_bucket.js +2 -2
  57. package/src/data/bucket/line_bucket.js +1 -1
  58. package/src/data/bucket/symbol_bucket.js +3 -3
  59. package/src/data/feature_index.js +1 -1
  60. package/src/data/program_configuration.js +1 -1
  61. package/src/geo/transform.js +1 -1
  62. package/src/gl/color_mode.js +1 -1
  63. package/src/gl/value.js +1 -1
  64. package/src/index.js +2 -2
  65. package/src/render/draw_debug.js +1 -1
  66. package/src/render/draw_fill.js +1 -1
  67. package/src/render/draw_heatmap.js +1 -1
  68. package/src/render/glyph_manager.js +6 -130
  69. package/src/render/painter.js +1 -1
  70. package/src/render/uniform_binding.js +1 -1
  71. package/src/shaders/index.js +50 -50
  72. package/src/source/geojson_source.js +1 -1
  73. package/src/source/geojson_worker_source.js +4 -4
  74. package/src/source/geojson_wrapper.js +35 -27
  75. package/src/source/image_source.js +2 -2
  76. package/src/source/raster_tile_source.js +1 -1
  77. package/src/source/resources/glyphs.js +73 -0
  78. package/src/source/resources/images.js +68 -0
  79. package/src/source/resources/index.js +22 -0
  80. package/src/source/rtl_text_plugin.js +1 -1
  81. package/src/source/source_cache.js +2 -2
  82. package/src/source/tile.js +1 -1
  83. package/src/source/vector_tile_source.js +1 -1
  84. package/src/source/vector_tile_worker_source.js +5 -5
  85. package/src/source/worker.js +6 -5
  86. package/src/source/worker_tile.js +4 -4
  87. package/src/style/light.js +1 -1
  88. package/src/style/properties.js +1 -2
  89. package/src/style/query_utils.js +1 -1
  90. package/src/style/style.js +4 -4
  91. package/src/style/style_layer/circle_style_layer.js +1 -1
  92. package/src/style/style_layer/fill_extrusion_style_layer.js +1 -1
  93. package/src/style/style_layer/line_style_layer.js +1 -1
  94. package/src/style/style_layer/symbol_style_layer.js +1 -1
  95. package/src/style/style_layer.js +2 -2
  96. package/src/style-spec/feature_filter/index.js +43 -34
  97. package/src/style-spec/group_by_layout.js +10 -32
  98. package/src/symbol/anchor.js +1 -1
  99. package/src/symbol/clip_line.js +1 -1
  100. package/src/symbol/collision_feature.js +1 -1
  101. package/src/symbol/collision_index.js +1 -1
  102. package/src/symbol/mergelines.js +0 -2
  103. package/src/symbol/projection.js +1 -1
  104. package/src/symbol/quads.js +1 -1
  105. package/src/symbol/symbol_layout.js +0 -1
  106. package/src/symbol/symbol_size.js +1 -1
  107. package/src/symbol/transform_text.js +0 -1
  108. package/src/ui/camera.js +2 -2
  109. package/src/ui/map.js +11 -41
  110. package/src/util/browser.js +3 -18
  111. package/src/util/classify_rings.js +1 -1
  112. package/src/util/config.js +1 -1
  113. package/src/util/dom.js +0 -88
  114. package/src/util/find_pole_of_inaccessibility.js +3 -3
  115. package/src/util/intersection_tests.js +1 -1
  116. package/src/util/web_worker_transfer.js +4 -4
  117. package/src/worker.js +4 -0
  118. package/build/min/src/shaders/background.fragment.glsl.txt +0 -5
  119. package/build/min/src/shaders/background.vertex.glsl.txt +0 -1
  120. package/build/min/src/shaders/background_pattern.fragment.glsl.txt +0 -5
  121. package/build/min/src/shaders/background_pattern.vertex.glsl.txt +0 -1
  122. package/build/min/src/shaders/clipping_mask.fragment.glsl.txt +0 -1
  123. package/build/min/src/shaders/clipping_mask.vertex.glsl.txt +0 -1
  124. package/build/min/src/shaders/collision_box.fragment.glsl.txt +0 -1
  125. package/build/min/src/shaders/collision_box.vertex.glsl.txt +0 -1
  126. package/build/min/src/shaders/collision_circle.fragment.glsl.txt +0 -1
  127. package/build/min/src/shaders/collision_circle.vertex.glsl.txt +0 -1
  128. package/build/min/src/shaders/debug.fragment.glsl.txt +0 -1
  129. package/build/min/src/shaders/debug.vertex.glsl.txt +0 -1
  130. package/build/min/src/shaders/fill_extrusion.fragment.glsl.txt +0 -5
  131. package/build/min/src/shaders/heatmap_texture.fragment.glsl.txt +0 -5
  132. package/build/min/src/shaders/heatmap_texture.vertex.glsl.txt +0 -1
  133. package/build/min/src/shaders/hillshade.vertex.glsl.txt +0 -1
  134. package/build/min/src/shaders/hillshade_prepare.vertex.glsl.txt +0 -1
  135. package/build/min/src/shaders/raster.fragment.glsl.txt +0 -5
  136. package/build/min/src/shaders/raster.vertex.glsl.txt +0 -1
  137. package/src/style/load_glyph_range.js +0 -17
  138. package/src/style-spec/expression/compound_expression.js +0 -132
  139. package/src/style-spec/expression/definitions/assertion.js +0 -116
  140. package/src/style-spec/expression/definitions/at.js +0 -57
  141. package/src/style-spec/expression/definitions/case.js +0 -73
  142. package/src/style-spec/expression/definitions/coalesce.js +0 -66
  143. package/src/style-spec/expression/definitions/coercion.js +0 -120
  144. package/src/style-spec/expression/definitions/collator.js +0 -80
  145. package/src/style-spec/expression/definitions/comparison.js +0 -193
  146. package/src/style-spec/expression/definitions/format.js +0 -97
  147. package/src/style-spec/expression/definitions/index.js +0 -339
  148. package/src/style-spec/expression/definitions/interpolate.js +0 -245
  149. package/src/style-spec/expression/definitions/length.js +0 -54
  150. package/src/style-spec/expression/definitions/let.js +0 -60
  151. package/src/style-spec/expression/definitions/literal.js +0 -69
  152. package/src/style-spec/expression/definitions/match.js +0 -142
  153. package/src/style-spec/expression/definitions/step.js +0 -116
  154. package/src/style-spec/expression/definitions/var.js +0 -38
  155. package/src/style-spec/expression/evaluation_context.js +0 -38
  156. package/src/style-spec/expression/index.js +0 -330
  157. package/src/style-spec/expression/is_constant.js +0 -63
  158. package/src/style-spec/expression/parsing_context.js +0 -221
  159. package/src/style-spec/expression/parsing_error.js +0 -9
  160. package/src/style-spec/expression/runtime_error.js +0 -12
  161. package/src/style-spec/expression/scope.js +0 -34
  162. package/src/style-spec/expression/stops.js +0 -37
  163. package/src/style-spec/expression/types/collator.js +0 -24
  164. package/src/style-spec/expression/types/formatted.js +0 -39
  165. package/src/style-spec/expression/types.js +0 -88
  166. package/src/style-spec/expression/values.js +0 -149
  167. package/src/style-spec/function/convert.js +0 -240
  168. package/src/style-spec/function/index.js +0 -303
  169. package/src/style-spec/util/color.js +0 -73
  170. package/src/style-spec/util/color_spaces.js +0 -128
  171. package/src/style-spec/util/get_type.js +0 -18
  172. package/src/style-spec/util/interpolate.js +0 -21
  173. package/src/style-spec/util/properties.js +0 -17
  174. package/src/style-spec/util/result.js +0 -19
  175. package/src/ui/anchor.js +0 -24
  176. package/src/ui/bind_handlers.js +0 -199
  177. package/src/ui/events.js +0 -210
  178. package/src/ui/handler/box_zoom.js +0 -151
  179. package/src/ui/handler/dblclick_zoom.js +0 -91
  180. package/src/ui/handler/drag_pan.js +0 -285
  181. package/src/ui/handler/drag_rotate.js +0 -290
  182. package/src/ui/handler/frame.js +0 -28
  183. package/src/ui/handler/inertia.js +0 -45
  184. package/src/ui/handler/keyboard.js +0 -148
  185. package/src/ui/handler/scroll_zoom.js +0 -284
  186. package/src/ui/handler/touch_zoom_rotate.js +0 -263
  187. package/src/util/evented.js +0 -182
@@ -1,116 +0,0 @@
1
- const assert = require('assert');
2
-
3
- const {
4
- ObjectType,
5
- ValueType,
6
- StringType,
7
- NumberType,
8
- BooleanType,
9
- checkSubtype,
10
- toString,
11
- array
12
- } = require('../types');
13
- const RuntimeError = require('../runtime_error');
14
- const { typeOf } = require('../values');
15
-
16
- const types = {
17
- string: StringType,
18
- number: NumberType,
19
- boolean: BooleanType,
20
- object: ObjectType
21
- };
22
-
23
- class Assertion {
24
- constructor(type, args) {
25
- this.type = type;
26
- this.args = args;
27
- }
28
-
29
- static parse(args, context) {
30
- if (args.length < 2) return context.error('Expected at least one argument.');
31
-
32
- let i = 1;
33
- let type;
34
-
35
- const name = args[0];
36
- if (name === 'array') {
37
- let itemType;
38
- if (args.length > 2) {
39
- const type = args[1];
40
- if (typeof type !== 'string' || !(type in types) || type === 'object')
41
- return context.error('The item type argument of "array" must be one of string, number, boolean', 1);
42
- itemType = types[type];
43
- i++;
44
- } else {
45
- itemType = ValueType;
46
- }
47
-
48
- let N;
49
- if (args.length > 3) {
50
- if (args[2] !== null && (typeof args[2] !== 'number' || args[2] < 0 || args[2] !== Math.floor(args[2]))) {
51
- return context.error('The length argument to "array" must be a positive integer literal', 2);
52
- }
53
- N = args[2];
54
- i++;
55
- }
56
-
57
- type = array(itemType, N);
58
- } else {
59
- assert(types[name], name);
60
- type = types[name];
61
- }
62
-
63
- const parsed = [];
64
- for (; i < args.length; i++) {
65
- const input = context.parse(args[i], i, ValueType);
66
- if (!input) return null;
67
- parsed.push(input);
68
- }
69
-
70
- return new Assertion(type, parsed);
71
- }
72
-
73
- evaluate(ctx) {
74
- for (let i = 0; i < this.args.length; i++) {
75
- const value = this.args[i].evaluate(ctx);
76
- const error = checkSubtype(this.type, typeOf(value));
77
- if (!error) {
78
- return value;
79
- }
80
- if (i === this.args.length - 1) {
81
- throw new RuntimeError(
82
- `Expected value to be of type ${toString(this.type)}, but found ${toString(typeOf(value))} instead.`
83
- );
84
- }
85
- }
86
-
87
- assert(false);
88
- return null;
89
- }
90
-
91
- eachChild(fn) {
92
- this.args.forEach(fn);
93
- }
94
-
95
- possibleOutputs() {
96
- return [].concat(...this.args.map(arg => arg.possibleOutputs()));
97
- }
98
-
99
- serialize() {
100
- const type = this.type;
101
- const serialized = [type.kind];
102
- if (type.kind === 'array') {
103
- const itemType = type.itemType;
104
- if (itemType.kind === 'string' || itemType.kind === 'number' || itemType.kind === 'boolean') {
105
- serialized.push(itemType.kind);
106
- const N = type.N;
107
- if (typeof N === 'number' || this.args.length > 1) {
108
- serialized.push(N);
109
- }
110
- }
111
- }
112
- return serialized.concat(this.args.map(arg => arg.serialize()));
113
- }
114
- }
115
-
116
- module.exports = Assertion;
@@ -1,57 +0,0 @@
1
- const { array, ValueType, NumberType } = require('../types');
2
-
3
- const RuntimeError = require('../runtime_error');
4
-
5
- class At {
6
- constructor(type, index, input) {
7
- this.type = type;
8
- this.index = index;
9
- this.input = input;
10
- }
11
-
12
- static parse(args, context) {
13
- if (args.length !== 3) return context.error(`Expected 2 arguments, but found ${args.length - 1} instead.`);
14
-
15
- const index = context.parse(args[1], 1, NumberType);
16
- const input = context.parse(args[2], 2, array(context.expectedType || ValueType));
17
-
18
- if (!index || !input) return null;
19
-
20
- const t = input.type;
21
- return new At(t.itemType, index, input);
22
- }
23
-
24
- evaluate(ctx) {
25
- const index = this.index.evaluate(ctx);
26
- const array = this.input.evaluate(ctx);
27
-
28
- if (index < 0) {
29
- throw new RuntimeError(`Array index out of bounds: ${index} < 0.`);
30
- }
31
-
32
- if (index >= array.length) {
33
- throw new RuntimeError(`Array index out of bounds: ${index} > ${array.length - 1}.`);
34
- }
35
-
36
- if (index !== Math.floor(index)) {
37
- throw new RuntimeError(`Array index must be an integer, but found ${index} instead.`);
38
- }
39
-
40
- return array[index];
41
- }
42
-
43
- eachChild(fn) {
44
- fn(this.index);
45
- fn(this.input);
46
- }
47
-
48
- possibleOutputs() {
49
- return [undefined];
50
- }
51
-
52
- serialize() {
53
- return ['at', this.index.serialize(), this.input.serialize()];
54
- }
55
- }
56
-
57
- module.exports = At;
@@ -1,73 +0,0 @@
1
- const assert = require('assert');
2
-
3
- const { BooleanType } = require('../types');
4
-
5
- class Case {
6
- constructor(type, branches, otherwise) {
7
- this.type = type;
8
- this.branches = branches;
9
- this.otherwise = otherwise;
10
- }
11
-
12
- static parse(args, context) {
13
- if (args.length < 4) return context.error(`Expected at least 3 arguments, but found only ${args.length - 1}.`);
14
- if (args.length % 2 !== 0) return context.error('Expected an odd number of arguments.');
15
-
16
- let outputType;
17
- if (context.expectedType && context.expectedType.kind !== 'value') {
18
- outputType = context.expectedType;
19
- }
20
-
21
- const branches = [];
22
- for (let i = 1; i < args.length - 1; i += 2) {
23
- const test = context.parse(args[i], i, BooleanType);
24
- if (!test) return null;
25
-
26
- const result = context.parse(args[i + 1], i + 1, outputType);
27
- if (!result) return null;
28
-
29
- branches.push([test, result]);
30
-
31
- outputType = outputType || result.type;
32
- }
33
-
34
- const otherwise = context.parse(args[args.length - 1], args.length - 1, outputType);
35
- if (!otherwise) return null;
36
-
37
- assert(outputType);
38
- return new Case(outputType, branches, otherwise);
39
- }
40
-
41
- evaluate(ctx) {
42
- for (const [test, expression] of this.branches) {
43
- if (test.evaluate(ctx)) {
44
- return expression.evaluate(ctx);
45
- }
46
- }
47
- return this.otherwise.evaluate(ctx);
48
- }
49
-
50
- eachChild(fn) {
51
- for (const [test, expression] of this.branches) {
52
- fn(test);
53
- fn(expression);
54
- }
55
- fn(this.otherwise);
56
- }
57
-
58
- possibleOutputs() {
59
- return []
60
- .concat(...this.branches.map(branch => branch[1].possibleOutputs()))
61
- .concat(this.otherwise.possibleOutputs());
62
- }
63
-
64
- serialize() {
65
- const serialized = ['case'];
66
- this.eachChild(child => {
67
- serialized.push(child.serialize());
68
- });
69
- return serialized;
70
- }
71
- }
72
-
73
- module.exports = Case;
@@ -1,66 +0,0 @@
1
- const assert = require('assert');
2
-
3
- const { checkSubtype, ValueType } = require('../types');
4
-
5
- class Coalesce {
6
- constructor(type, args) {
7
- this.type = type;
8
- this.args = args;
9
- }
10
-
11
- static parse(args, context) {
12
- if (args.length < 2) {
13
- return context.error('Expectected at least one argument.');
14
- }
15
- let outputType = null;
16
- const expectedType = context.expectedType;
17
- if (expectedType && expectedType.kind !== 'value') {
18
- outputType = expectedType;
19
- }
20
- const parsedArgs = [];
21
-
22
- for (const arg of args.slice(1)) {
23
- const parsed = context.parse(arg, 1 + parsedArgs.length, outputType, undefined, { typeAnnotation: 'omit' });
24
- if (!parsed) return null;
25
- outputType = outputType || parsed.type;
26
- parsedArgs.push(parsed);
27
- }
28
- assert(outputType);
29
-
30
- // Above, we parse arguments without inferred type annotation so that
31
- // they don't produce a runtime error for `null` input, which would
32
- // preempt the desired null-coalescing behavior.
33
- // Thus, if any of our arguments would have needed an annotation, we
34
- // need to wrap the enclosing coalesce expression with it instead.
35
- const needsAnnotation = expectedType && parsedArgs.some(arg => checkSubtype(expectedType, arg.type));
36
-
37
- return needsAnnotation ? new Coalesce(ValueType, parsedArgs) : new Coalesce(outputType, parsedArgs);
38
- }
39
-
40
- evaluate(ctx) {
41
- let result = null;
42
- for (const arg of this.args) {
43
- result = arg.evaluate(ctx);
44
- if (result !== null) break;
45
- }
46
- return result;
47
- }
48
-
49
- eachChild(fn) {
50
- this.args.forEach(fn);
51
- }
52
-
53
- possibleOutputs() {
54
- return [].concat(...this.args.map(arg => arg.possibleOutputs()));
55
- }
56
-
57
- serialize() {
58
- const serialized = ['coalesce'];
59
- this.eachChild(child => {
60
- serialized.push(child.serialize());
61
- });
62
- return serialized;
63
- }
64
- }
65
-
66
- module.exports = Coalesce;
@@ -1,120 +0,0 @@
1
- const assert = require('assert');
2
-
3
- const { BooleanType, ColorType, NumberType, StringType, ValueType } = require('../types');
4
- const { Color, toString: valueToString, validateRGBA } = require('../values');
5
- const RuntimeError = require('../runtime_error');
6
- const { FormatExpression } = require('../definitions/format');
7
- const { Formatted } = require('../types/formatted');
8
-
9
- const types = {
10
- 'to-boolean': BooleanType,
11
- 'to-color': ColorType,
12
- 'to-number': NumberType,
13
- 'to-string': StringType
14
- };
15
-
16
- /**
17
- * Special form for error-coalescing coercion expressions "to-number",
18
- * "to-color". Since these coercions can fail at runtime, they accept multiple
19
- * arguments, only evaluating one at a time until one succeeds.
20
- *
21
- * @private
22
- */
23
- class Coercion {
24
- constructor(type, args) {
25
- this.type = type;
26
- this.args = args;
27
- }
28
-
29
- static parse(args, context) {
30
- if (args.length < 2) return context.error('Expected at least one argument.');
31
-
32
- const name = args[0];
33
- assert(types[name], name);
34
-
35
- if ((name === 'to-boolean' || name === 'to-string') && args.length !== 2)
36
- return context.error('Expected one argument.');
37
-
38
- const type = types[name];
39
-
40
- const parsed = [];
41
- for (let i = 1; i < args.length; i++) {
42
- const input = context.parse(args[i], i, ValueType);
43
- if (!input) return null;
44
- parsed.push(input);
45
- }
46
-
47
- return new Coercion(type, parsed);
48
- }
49
-
50
- evaluate(ctx) {
51
- if (this.type.kind === 'boolean') {
52
- return Boolean(this.args[0].evaluate(ctx));
53
- }
54
- if (this.type.kind === 'color') {
55
- let input;
56
- let error;
57
- for (const arg of this.args) {
58
- input = arg.evaluate(ctx);
59
- error = null;
60
- if (input instanceof Color) {
61
- return input;
62
- }
63
- if (typeof input === 'string') {
64
- const c = ctx.parseColor(input);
65
- if (c) return c;
66
- } else if (Array.isArray(input)) {
67
- if (input.length < 3 || input.length > 4) {
68
- error = `Invalid rbga value ${JSON.stringify(input)}: expected an array containing either three or four numeric values.`;
69
- } else {
70
- error = validateRGBA(input[0], input[1], input[2], input[3]);
71
- }
72
- if (!error) {
73
- return new Color(input[0] / 255, input[1] / 255, input[2] / 255, input[3]);
74
- }
75
- }
76
- }
77
- throw new RuntimeError(
78
- error || `Could not parse color from value '${typeof input === 'string' ? input : JSON.stringify(input)}'`
79
- );
80
- }
81
- if (this.type.kind === 'number') {
82
- let value = null;
83
- for (const arg of this.args) {
84
- value = arg.evaluate(ctx);
85
- if (value === null) return 0;
86
- const num = Number(value);
87
- if (isNaN(num)) continue;
88
- return num;
89
- }
90
- throw new RuntimeError(`Could not convert ${JSON.stringify(value)} to number.`);
91
- }
92
- if (this.type.kind === 'formatted') {
93
- // There is no explicit 'to-formatted' but this coercion can be implicitly
94
- // created by properties that expect the 'formatted' type.
95
- return Formatted.fromString(valueToString(this.args[0].evaluate(ctx)));
96
- }
97
- return valueToString(this.args[0].evaluate(ctx));
98
- }
99
-
100
- eachChild(fn) {
101
- this.args.forEach(fn);
102
- }
103
-
104
- possibleOutputs() {
105
- return [].concat(...this.args.map(arg => arg.possibleOutputs()));
106
- }
107
-
108
- serialize() {
109
- if (this.type.kind === 'formatted') {
110
- return new FormatExpression([{ text: this.args[0], scale: null, font: null }]).serialize();
111
- }
112
- const serialized = [`to-${this.type.kind}`];
113
- this.eachChild(child => {
114
- serialized.push(child.serialize());
115
- });
116
- return serialized;
117
- }
118
- }
119
-
120
- module.exports = Coercion;
@@ -1,80 +0,0 @@
1
- const { StringType, BooleanType, CollatorType } = require('../types');
2
- const { Collator } = require('../types/collator');
3
-
4
- class CollatorExpression {
5
- constructor(caseSensitive, diacriticSensitive, locale) {
6
- this.type = CollatorType;
7
- this.locale = locale;
8
- this.caseSensitive = caseSensitive;
9
- this.diacriticSensitive = diacriticSensitive;
10
- }
11
-
12
- static parse(args, context) {
13
- if (args.length !== 2) return context.error('Expected one argument.');
14
-
15
- const options = args[1];
16
- if (typeof options !== 'object' || Array.isArray(options))
17
- return context.error('Collator options argument must be an object.');
18
-
19
- const caseSensitive = context.parse(
20
- options['case-sensitive'] === undefined ? false : options['case-sensitive'],
21
- 1,
22
- BooleanType
23
- );
24
- if (!caseSensitive) return null;
25
-
26
- const diacriticSensitive = context.parse(
27
- options['diacritic-sensitive'] === undefined ? false : options['diacritic-sensitive'],
28
- 1,
29
- BooleanType
30
- );
31
- if (!diacriticSensitive) return null;
32
-
33
- let locale = null;
34
- if (options['locale']) {
35
- locale = context.parse(options['locale'], 1, StringType);
36
- if (!locale) return null;
37
- }
38
-
39
- return new CollatorExpression(caseSensitive, diacriticSensitive, locale);
40
- }
41
-
42
- evaluate(ctx) {
43
- return new Collator(
44
- this.caseSensitive.evaluate(ctx),
45
- this.diacriticSensitive.evaluate(ctx),
46
- this.locale ? this.locale.evaluate(ctx) : null
47
- );
48
- }
49
-
50
- eachChild(fn) {
51
- fn(this.caseSensitive);
52
- fn(this.diacriticSensitive);
53
- if (this.locale) {
54
- fn(this.locale);
55
- }
56
- }
57
-
58
- possibleOutputs() {
59
- // Technically the set of possible outputs is the combinatoric set of Collators produced
60
- // by all possibleOutputs of locale/caseSensitive/diacriticSensitive
61
- // But for the primary use of Collators in comparison operators, we ignore the Collator's
62
- // possibleOutputs anyway, so we can get away with leaving this undefined for now.
63
- return [undefined];
64
- }
65
-
66
- serialize() {
67
- const options = {};
68
- options['case-sensitive'] = this.caseSensitive.serialize();
69
- options['diacritic-sensitive'] = this.diacriticSensitive.serialize();
70
- if (this.locale) {
71
- options['locale'] = this.locale.serialize();
72
- }
73
- return ['collator', options];
74
- }
75
- }
76
-
77
- module.exports = {
78
- Collator,
79
- CollatorExpression
80
- };
@@ -1,193 +0,0 @@
1
- const { toString, ValueType, BooleanType, CollatorType } = require('../types');
2
- const Assertion = require('./assertion');
3
- const { typeOf } = require('../values');
4
- const RuntimeError = require('../runtime_error');
5
-
6
- function isComparableType(op, type) {
7
- if (op === '==' || op === '!=') {
8
- // equality operator
9
- return (
10
- type.kind === 'boolean' ||
11
- type.kind === 'string' ||
12
- type.kind === 'number' ||
13
- type.kind === 'null' ||
14
- type.kind === 'value'
15
- );
16
- }
17
- // ordering operator
18
- return type.kind === 'string' || type.kind === 'number' || type.kind === 'value';
19
- }
20
-
21
- function eq(ctx, a, b) {
22
- return a === b;
23
- }
24
- function neq(ctx, a, b) {
25
- return a !== b;
26
- }
27
- function lt(ctx, a, b) {
28
- return a < b;
29
- }
30
- function gt(ctx, a, b) {
31
- return a > b;
32
- }
33
- function lteq(ctx, a, b) {
34
- return a <= b;
35
- }
36
- function gteq(ctx, a, b) {
37
- return a >= b;
38
- }
39
-
40
- function eqCollate(ctx, a, b, c) {
41
- return c.compare(a, b) === 0;
42
- }
43
- function neqCollate(ctx, a, b, c) {
44
- return !eqCollate(ctx, a, b, c);
45
- }
46
- function ltCollate(ctx, a, b, c) {
47
- return c.compare(a, b) < 0;
48
- }
49
- function gtCollate(ctx, a, b, c) {
50
- return c.compare(a, b) > 0;
51
- }
52
- function lteqCollate(ctx, a, b, c) {
53
- return c.compare(a, b) <= 0;
54
- }
55
- function gteqCollate(ctx, a, b, c) {
56
- return c.compare(a, b) >= 0;
57
- }
58
-
59
- /**
60
- * Special form for comparison operators, implementing the signatures:
61
- * - (T, T, ?Collator) => boolean
62
- * - (T, value, ?Collator) => boolean
63
- * - (value, T, ?Collator) => boolean
64
- *
65
- * For inequalities, T must be either value, string, or number. For ==/!=, it
66
- * can also be boolean or null.
67
- *
68
- * Equality semantics are equivalent to Javascript's strict equality (===/!==)
69
- * -- i.e., when the arguments' types don't match, == evaluates to false, != to
70
- * true.
71
- *
72
- * When types don't match in an ordering comparison, a runtime error is thrown.
73
- *
74
- * @private
75
- */
76
- function makeComparison(op, compareBasic, compareWithCollator) {
77
- const isOrderComparison = op !== '==' && op !== '!=';
78
-
79
- return class Comparison {
80
- constructor(lhs, rhs, collator) {
81
- this.type = BooleanType;
82
- this.lhs = lhs;
83
- this.rhs = rhs;
84
- this.collator = collator;
85
- this.hasUntypedArgument = lhs.type.kind === 'value' || rhs.type.kind === 'value';
86
- }
87
-
88
- static parse(args, context) {
89
- if (args.length !== 3 && args.length !== 4) return context.error('Expected two or three arguments.');
90
-
91
- const op = args[0];
92
-
93
- let lhs = context.parse(args[1], 1, ValueType);
94
- if (!lhs) return null;
95
- if (!isComparableType(op, lhs.type)) {
96
- return context.concat(1).error(`"${op}" comparisons are not supported for type '${toString(lhs.type)}'.`);
97
- }
98
- let rhs = context.parse(args[2], 2, ValueType);
99
- if (!rhs) return null;
100
- if (!isComparableType(op, rhs.type)) {
101
- return context.concat(2).error(`"${op}" comparisons are not supported for type '${toString(rhs.type)}'.`);
102
- }
103
-
104
- if (lhs.type.kind !== rhs.type.kind && lhs.type.kind !== 'value' && rhs.type.kind !== 'value') {
105
- return context.error(`Cannot compare types '${toString(lhs.type)}' and '${toString(rhs.type)}'.`);
106
- }
107
-
108
- if (isOrderComparison) {
109
- // typing rules specific to less/greater than operators
110
- if (lhs.type.kind === 'value' && rhs.type.kind !== 'value') {
111
- // (value, T)
112
- lhs = new Assertion(rhs.type, [lhs]);
113
- } else if (lhs.type.kind !== 'value' && rhs.type.kind === 'value') {
114
- // (T, value)
115
- rhs = new Assertion(lhs.type, [rhs]);
116
- }
117
- }
118
-
119
- let collator = null;
120
- if (args.length === 4) {
121
- if (
122
- lhs.type.kind !== 'string' &&
123
- rhs.type.kind !== 'string' &&
124
- lhs.type.kind !== 'value' &&
125
- rhs.type.kind !== 'value'
126
- ) {
127
- return context.error('Cannot use collator to compare non-string types.');
128
- }
129
- collator = context.parse(args[3], 3, CollatorType);
130
- if (!collator) return null;
131
- }
132
-
133
- return new Comparison(lhs, rhs, collator);
134
- }
135
-
136
- evaluate(ctx) {
137
- const lhs = this.lhs.evaluate(ctx);
138
- const rhs = this.rhs.evaluate(ctx);
139
-
140
- if (isOrderComparison && this.hasUntypedArgument) {
141
- const lt = typeOf(lhs);
142
- const rt = typeOf(rhs);
143
- // check that type is string or number, and equal
144
- if (lt.kind !== rt.kind || !(lt.kind === 'string' || lt.kind === 'number')) {
145
- throw new RuntimeError(
146
- `Expected arguments for "${op}" to be (string, string) or (number, number), but found (${lt.kind}, ${rt.kind}) instead.`
147
- );
148
- }
149
- }
150
-
151
- if (this.collator && !isOrderComparison && this.hasUntypedArgument) {
152
- const lt = typeOf(lhs);
153
- const rt = typeOf(rhs);
154
- if (lt.kind !== 'string' || rt.kind !== 'string') {
155
- return compareBasic(ctx, lhs, rhs);
156
- }
157
- }
158
-
159
- return this.collator
160
- ? compareWithCollator(ctx, lhs, rhs, this.collator.evaluate(ctx))
161
- : compareBasic(ctx, lhs, rhs);
162
- }
163
-
164
- eachChild(fn) {
165
- fn(this.lhs);
166
- fn(this.rhs);
167
- if (this.collator) {
168
- fn(this.collator);
169
- }
170
- }
171
-
172
- possibleOutputs() {
173
- return [true, false];
174
- }
175
-
176
- serialize() {
177
- const serialized = [op];
178
- this.eachChild(child => {
179
- serialized.push(child.serialize());
180
- });
181
- return serialized;
182
- }
183
- };
184
- }
185
-
186
- module.exports = {
187
- Equals: makeComparison('==', eq, eqCollate),
188
- NotEquals: makeComparison('!=', neq, neqCollate),
189
- LessThan: makeComparison('<', lt, ltCollate),
190
- GreaterThan: makeComparison('>', gt, gtCollate),
191
- LessThanOrEqual: makeComparison('<=', lteq, lteqCollate),
192
- GreaterThanOrEqual: makeComparison('>=', gteq, gteqCollate)
193
- };