@versatiles/style 3.8.0 → 3.8.2
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/README.MD +13 -7
- package/dist/lib/decorator.js +1 -1
- package/dist/lib/decorator.test.js +1 -1
- package/dist/lib/style_builder.js +1 -9
- package/dist/lib/style_guesser.js +6 -1
- package/dist/lib/style_guesser.test.js +18 -0
- package/dist/lib/utils.d.ts +1 -0
- package/dist/lib/utils.js +8 -0
- package/dist/lib/utils.test.js +112 -94
- package/package.json +17 -17
package/README.MD
CHANGED
|
@@ -35,12 +35,18 @@ Use it in:
|
|
|
35
35
|
<script src="maplibre-gl.js"></script>
|
|
36
36
|
<script src="versatiles-style.js"></script>
|
|
37
37
|
<script>
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
const style = VersaTilesStyle.graybeard({
|
|
39
|
+
tiles: ['https://???.org/tiles/osm/{z}/{x}/{y}'],
|
|
40
|
+
sprite: 'https://???.org/assets/styles/swr-bright/sprite',
|
|
41
|
+
glyphs: 'https://???.org/assets/fonts/{fontstack}/{range}.pbf',
|
|
42
|
+
languageSuffix: '_de',
|
|
43
|
+
colors: { label: '#222' },
|
|
44
|
+
recolor: { gamma: 0.5 }
|
|
45
|
+
});
|
|
40
46
|
|
|
41
47
|
const map = new maplibregl.Map({
|
|
42
48
|
container: 'map',
|
|
43
|
-
style
|
|
49
|
+
style
|
|
44
50
|
});
|
|
45
51
|
<script>
|
|
46
52
|
```
|
|
@@ -125,7 +131,7 @@ interface {
|
|
|
125
131
|
|
|
126
132
|
### Method: `colorful(options)`
|
|
127
133
|
|
|
128
|
-
<sup><a href="https://github.com/versatiles-org/versatiles-style/blob/
|
|
134
|
+
<sup><a href="https://github.com/versatiles-org/versatiles-style/blob/be6dcfe/src/index.ts#L4">\[src]</a></sup>
|
|
129
135
|
|
|
130
136
|
**Parameters:**
|
|
131
137
|
|
|
@@ -135,7 +141,7 @@ interface {
|
|
|
135
141
|
|
|
136
142
|
### Method: `graybeard(options)`
|
|
137
143
|
|
|
138
|
-
<sup><a href="https://github.com/versatiles-org/versatiles-style/blob/
|
|
144
|
+
<sup><a href="https://github.com/versatiles-org/versatiles-style/blob/be6dcfe/src/index.ts#L9">\[src]</a></sup>
|
|
139
145
|
|
|
140
146
|
**Parameters:**
|
|
141
147
|
|
|
@@ -145,7 +151,7 @@ interface {
|
|
|
145
151
|
|
|
146
152
|
### Method: `guessStyle(opt)`
|
|
147
153
|
|
|
148
|
-
<sup><a href="https://github.com/versatiles-org/versatiles-style/blob/
|
|
154
|
+
<sup><a href="https://github.com/versatiles-org/versatiles-style/blob/be6dcfe/src/lib/style_guesser.ts#L13">\[src]</a></sup>
|
|
149
155
|
|
|
150
156
|
**Parameters:**
|
|
151
157
|
|
|
@@ -155,7 +161,7 @@ interface {
|
|
|
155
161
|
|
|
156
162
|
### Method: `neutrino(options)`
|
|
157
163
|
|
|
158
|
-
<sup><a href="https://github.com/versatiles-org/versatiles-style/blob/
|
|
164
|
+
<sup><a href="https://github.com/versatiles-org/versatiles-style/blob/be6dcfe/src/index.ts#L14">\[src]</a></sup>
|
|
159
165
|
|
|
160
166
|
**Parameters:**
|
|
161
167
|
|
package/dist/lib/decorator.js
CHANGED
|
@@ -98,7 +98,7 @@ function processColor(value) {
|
|
|
98
98
|
if (typeof value === 'string')
|
|
99
99
|
value = Color(value);
|
|
100
100
|
if (value instanceof Color) {
|
|
101
|
-
value = (value.alpha() === 1) ? value.hex() : value.
|
|
101
|
+
value = (value.alpha() === 1) ? value.hex() : value.string();
|
|
102
102
|
return value.toLowerCase();
|
|
103
103
|
}
|
|
104
104
|
throw new Error(`unknown color type "${typeof value}"`);
|
|
@@ -27,7 +27,7 @@ describe('decorate function', () => {
|
|
|
27
27
|
expect(layer.layout).toHaveProperty('visibility', 'none');
|
|
28
28
|
}
|
|
29
29
|
if (layer.id === 'layer2') {
|
|
30
|
-
expect(layer.paint).toHaveProperty('line-color', '
|
|
30
|
+
expect(layer.paint).toHaveProperty('line-color', 'rgba(0, 255, 0, 0.5)');
|
|
31
31
|
expect(layer.layout).toHaveProperty('visibility', 'visible');
|
|
32
32
|
}
|
|
33
33
|
});
|
|
@@ -3,7 +3,7 @@ import getShortbreadTemplate from './shortbread/template.js';
|
|
|
3
3
|
import getShortbreadLayers from './shortbread/layers.js';
|
|
4
4
|
import { decorate } from './decorator.js';
|
|
5
5
|
import { getDefaultRecolorFlags, recolor } from './recolor.js';
|
|
6
|
-
import { deepClone } from './utils.js';
|
|
6
|
+
import { deepClone, resolveUrl } from './utils.js';
|
|
7
7
|
// Stylemaker class definition
|
|
8
8
|
export default class StyleBuilder {
|
|
9
9
|
#sourceName = 'versatiles-shortbread';
|
|
@@ -70,14 +70,6 @@ export default class StyleBuilder {
|
|
|
70
70
|
if ('tiles' in source)
|
|
71
71
|
source.tiles = tiles.map(url => resolveUrl(baseUrl, url));
|
|
72
72
|
return style;
|
|
73
|
-
function resolveUrl(base, url) {
|
|
74
|
-
if (!Boolean(base))
|
|
75
|
-
return url;
|
|
76
|
-
url = new URL(url, base).href;
|
|
77
|
-
url = url.replace(/%7B/gi, '{');
|
|
78
|
-
url = url.replace(/%7D/gi, '}');
|
|
79
|
-
return url;
|
|
80
|
-
}
|
|
81
73
|
}
|
|
82
74
|
getColors(colors) {
|
|
83
75
|
const entriesString = Object.entries(colors);
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { isTileJSONSpecification } from './types.js';
|
|
4
4
|
import randomColorGenerator from './random_color.js';
|
|
5
5
|
import Colorful from '../style/colorful.js';
|
|
6
|
+
import { resolveUrl } from './utils.js';
|
|
6
7
|
export default function guess(opt) {
|
|
7
8
|
const { format } = opt;
|
|
8
9
|
const tilejsonBasic = {
|
|
@@ -21,6 +22,10 @@ export default function guess(opt) {
|
|
|
21
22
|
name: opt.name,
|
|
22
23
|
template: opt.template,
|
|
23
24
|
};
|
|
25
|
+
const { baseUrl } = opt;
|
|
26
|
+
if (typeof baseUrl === 'string') {
|
|
27
|
+
tilejsonBasic.tiles = tilejsonBasic.tiles.map(url => resolveUrl(baseUrl, url));
|
|
28
|
+
}
|
|
24
29
|
let k;
|
|
25
30
|
for (k in tilejsonBasic) {
|
|
26
31
|
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
@@ -52,7 +57,7 @@ export default function guess(opt) {
|
|
|
52
57
|
break;
|
|
53
58
|
case 'vector':
|
|
54
59
|
if (isShortbread(tilejson)) {
|
|
55
|
-
style = getShortbreadStyle(tilejson,
|
|
60
|
+
style = getShortbreadStyle(tilejson, opt);
|
|
56
61
|
}
|
|
57
62
|
else {
|
|
58
63
|
style = getInspectorStyle(tilejson);
|
|
@@ -121,4 +121,22 @@ describe('guessStyle', () => {
|
|
|
121
121
|
});
|
|
122
122
|
});
|
|
123
123
|
});
|
|
124
|
+
describe('absolute tile urls override baseUrl', () => {
|
|
125
|
+
cases.forEach(({ type, options }) => {
|
|
126
|
+
it(type, () => {
|
|
127
|
+
const style = guessStyle({ ...options, tiles: ['https://example1.org/tiles/{z}/{x}/{y}'], baseUrl: 'https://example2.org/' });
|
|
128
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
129
|
+
expect(Object.values(style.sources)[0].tiles).toEqual(['https://example1.org/tiles/{z}/{x}/{y}']);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
describe('relative tile urls are resolved with baseUrl', () => {
|
|
134
|
+
cases.forEach(({ type, options }) => {
|
|
135
|
+
it(type, () => {
|
|
136
|
+
const style = guessStyle({ ...options, tiles: ['./{z}/{x}/{y}'], baseUrl: 'https://example2.org/tiles/' });
|
|
137
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
138
|
+
expect(Object.values(style.sources)[0].tiles).toEqual(['https://example2.org/tiles/{z}/{x}/{y}']);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
});
|
|
124
142
|
});
|
package/dist/lib/utils.d.ts
CHANGED
|
@@ -2,3 +2,4 @@ export declare function deepClone<T>(obj: T): T;
|
|
|
2
2
|
export declare function isSimpleObject(item: unknown): boolean;
|
|
3
3
|
export declare function isBasicType(item: unknown): boolean;
|
|
4
4
|
export declare function deepMerge<T extends object>(source0: T, ...sources: T[]): T;
|
|
5
|
+
export declare function resolveUrl(base: string, url: string): string;
|
package/dist/lib/utils.js
CHANGED
|
@@ -104,3 +104,11 @@ export function deepMerge(source0, ...sources) {
|
|
|
104
104
|
}
|
|
105
105
|
return target;
|
|
106
106
|
}
|
|
107
|
+
export function resolveUrl(base, url) {
|
|
108
|
+
if (!Boolean(base))
|
|
109
|
+
return url;
|
|
110
|
+
url = new URL(url, base).href;
|
|
111
|
+
url = url.replace(/%7B/gi, '{');
|
|
112
|
+
url = url.replace(/%7D/gi, '}');
|
|
113
|
+
return url;
|
|
114
|
+
}
|
package/dist/lib/utils.test.js
CHANGED
|
@@ -1,97 +1,115 @@
|
|
|
1
|
-
import { deepClone, isSimpleObject, isBasicType, deepMerge } from './utils.js';
|
|
1
|
+
import { deepClone, isSimpleObject, isBasicType, deepMerge, resolveUrl } from './utils.js';
|
|
2
2
|
import Color from 'color';
|
|
3
|
-
describe('
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
3
|
+
describe('deepClone', () => {
|
|
4
|
+
it('clones primitive types correctly', () => {
|
|
5
|
+
expect(deepClone(10)).toBe(10);
|
|
6
|
+
expect(deepClone(true)).toBe(true);
|
|
7
|
+
expect(deepClone('string')).toBe('string');
|
|
8
|
+
});
|
|
9
|
+
it('clones an array correctly', () => {
|
|
10
|
+
const array = [1, 2, { a: 'b' }];
|
|
11
|
+
const clonedArray = deepClone(array);
|
|
12
|
+
expect(clonedArray).toEqual(array);
|
|
13
|
+
expect(clonedArray).not.toBe(array);
|
|
14
|
+
});
|
|
15
|
+
it('clones a simple object correctly', () => {
|
|
16
|
+
const obj = { a: 'b', c: 1, d: true };
|
|
17
|
+
const clonedObj = deepClone(obj);
|
|
18
|
+
expect(clonedObj).toEqual(obj);
|
|
19
|
+
expect(clonedObj).not.toBe(obj);
|
|
20
|
+
});
|
|
21
|
+
it('clones a Color instance correctly', () => {
|
|
22
|
+
const color = new Color('#FF5733');
|
|
23
|
+
const clonedColor = deepClone(color);
|
|
24
|
+
expect(clonedColor.hex()).toBe(color.hex());
|
|
25
|
+
});
|
|
26
|
+
it('throws an error for non-implemented types', () => {
|
|
27
|
+
const func = () => true;
|
|
28
|
+
expect(() => deepClone(func)).toThrow('Not implemented yet: "function" case');
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
describe('isSimpleObject', () => {
|
|
32
|
+
it('identifies simple objects correctly', () => {
|
|
33
|
+
expect(isSimpleObject({ a: 1 })).toBe(true);
|
|
34
|
+
});
|
|
35
|
+
it('rejects non-objects', () => {
|
|
36
|
+
expect(isSimpleObject(1)).toBe(false);
|
|
37
|
+
expect(isSimpleObject('a')).toBe(false);
|
|
38
|
+
expect(isSimpleObject(true)).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
it('rejects arrays', () => {
|
|
41
|
+
expect(isSimpleObject([1, 2, 3])).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
it('rejects objects with prototype properties', () => {
|
|
44
|
+
class MyClass {
|
|
45
|
+
#property = true;
|
|
46
|
+
}
|
|
47
|
+
expect(isSimpleObject(new MyClass())).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
describe('isBasicType', () => {
|
|
51
|
+
it('returns true for basic types', () => {
|
|
52
|
+
expect(isBasicType(1)).toBe(true);
|
|
53
|
+
expect(isBasicType('string')).toBe(true);
|
|
54
|
+
expect(isBasicType(true)).toBe(true);
|
|
55
|
+
expect(isBasicType(undefined)).toBe(true);
|
|
56
|
+
});
|
|
57
|
+
it('returns false for objects', () => {
|
|
58
|
+
expect(isBasicType({})).toBe(false);
|
|
59
|
+
expect(isBasicType([])).toBe(false);
|
|
60
|
+
});
|
|
61
|
+
it('throws error for functions', () => {
|
|
62
|
+
expect(() => isBasicType(() => true)).toThrow();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
describe('deepMerge', () => {
|
|
66
|
+
it('merges simple objects correctly', () => {
|
|
67
|
+
const target = { a: 1, b: 2 };
|
|
68
|
+
const source = { b: 3, c: 4 };
|
|
69
|
+
const result = deepMerge(target, source);
|
|
70
|
+
expect(result).toEqual({ a: 1, b: 3, c: 4 });
|
|
71
|
+
});
|
|
72
|
+
it('merges nested objects correctly', () => {
|
|
73
|
+
const target = { a: { d: 4 }, b: 2 };
|
|
74
|
+
const source = { a: { e: 5 }, b: 3 };
|
|
75
|
+
const result = deepMerge(target, source);
|
|
76
|
+
expect(result).toEqual({ a: { d: 4, e: 5 }, b: 3 });
|
|
77
|
+
});
|
|
78
|
+
it('overrides primitives with object types', () => {
|
|
79
|
+
const target = { a: 1, b: 2 };
|
|
80
|
+
const source = { a: { d: 4 }, b: { e: 5 } };
|
|
81
|
+
const result = deepMerge(target, source);
|
|
82
|
+
expect(result).toEqual({ a: { d: 4 }, b: { e: 5 } });
|
|
83
|
+
});
|
|
84
|
+
it('merges Color instances correctly', () => {
|
|
85
|
+
const target = { color: new Color('#FF5733') };
|
|
86
|
+
const source = { color: new Color('#33FF57') };
|
|
87
|
+
const result = deepMerge(target, source);
|
|
88
|
+
expect(result.color.hex()).toBe(source.color.hex());
|
|
89
|
+
});
|
|
90
|
+
it('throws error for unpredicted cases', () => {
|
|
91
|
+
const target = { a: () => false };
|
|
92
|
+
const source = { a: { b: 1 } };
|
|
93
|
+
expect(() => deepMerge(target, source)).toThrow('Not implemented yet: "function" case');
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
describe('resolveUrl', () => {
|
|
97
|
+
test('should correctly resolve a relative URL with a base URL', () => {
|
|
98
|
+
expect(resolveUrl('http://example.com/', 'path/page')).toBe('http://example.com/path/page');
|
|
99
|
+
});
|
|
100
|
+
test('should return the same URL if base URL is empty', () => {
|
|
101
|
+
expect(resolveUrl('', 'http://example.com/path/page')).toBe('http://example.com/path/page');
|
|
102
|
+
});
|
|
103
|
+
test('should return the correct URL if absolute URL is used', () => {
|
|
104
|
+
expect(resolveUrl('http://example1.com', 'http://example.com/path/page')).toBe('http://example.com/path/page');
|
|
105
|
+
});
|
|
106
|
+
test('should handle URLs with special characters', () => {
|
|
107
|
+
expect(resolveUrl('http://example.com/', 'path/{param}')).toBe('http://example.com/path/{param}');
|
|
108
|
+
});
|
|
109
|
+
test('should handle URLs already containing encoded special characters', () => {
|
|
110
|
+
expect(resolveUrl('http://example.com/', 'path/%7Bparam%7D')).toBe('http://example.com/path/{param}');
|
|
111
|
+
});
|
|
112
|
+
test('should throw error for invalid base URL', () => {
|
|
113
|
+
expect(() => resolveUrl('invalid-base', 'path/page')).toThrow('Invalid URL');
|
|
96
114
|
});
|
|
97
115
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@versatiles/style",
|
|
3
|
-
"version": "3.8.
|
|
3
|
+
"version": "3.8.2",
|
|
4
4
|
"description": "Generate StyleJSON for MapLibre",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"build-styles": "tsx scripts/build-styles.ts",
|
|
13
13
|
"doc": "vrt ts2md src/index.ts tsconfig.node.json | vrt insertmd README.md '# API'",
|
|
14
14
|
"lint": "npm run build-browser && eslint --color .",
|
|
15
|
-
"release": "
|
|
15
|
+
"release": "npx vrt release-npm",
|
|
16
16
|
"test": "npm run test-typescript && npm run test-node && npm run test-browser",
|
|
17
17
|
"test-browser": "npm run build-browser && jest -c=jest.config.browser.ts",
|
|
18
18
|
"test-coverage": "NODE_OPTIONS=--experimental-vm-modules jest -c=jest.config.coverage.ts",
|
|
@@ -37,29 +37,29 @@
|
|
|
37
37
|
"dist/**/*.d.ts"
|
|
38
38
|
],
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@maplibre/maplibre-gl-style-spec": "^
|
|
40
|
+
"@maplibre/maplibre-gl-style-spec": "^20.1.1",
|
|
41
41
|
"@rollup/plugin-commonjs": "^25.0.7",
|
|
42
42
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
43
43
|
"@rollup/plugin-terser": "^0.4.4",
|
|
44
|
-
"@rollup/plugin-typescript": "^11.1.
|
|
44
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
45
45
|
"@types/brace-expansion": "^1.1.2",
|
|
46
46
|
"@types/inquirer": "^9.0.7",
|
|
47
|
-
"@types/jest": "^29.5.
|
|
48
|
-
"@types/node": "^20.
|
|
49
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
50
|
-
"@typescript-eslint/parser": "^
|
|
51
|
-
"@versatiles/release-tool": "^1.
|
|
52
|
-
"eslint": "^8.
|
|
53
|
-
"inquirer": "^9.2.
|
|
47
|
+
"@types/jest": "^29.5.12",
|
|
48
|
+
"@types/node": "^20.11.17",
|
|
49
|
+
"@typescript-eslint/eslint-plugin": "^7.0.1",
|
|
50
|
+
"@typescript-eslint/parser": "^7.0.1",
|
|
51
|
+
"@versatiles/release-tool": "^1.2.1",
|
|
52
|
+
"eslint": "^8.56.0",
|
|
53
|
+
"inquirer": "^9.2.14",
|
|
54
54
|
"jest": "^29.7.0",
|
|
55
55
|
"jest-environment-jsdom": "^29.7.0",
|
|
56
56
|
"jest-ts-webcompat-resolver": "^1.0.0",
|
|
57
|
-
"npm-check-updates": "^16.14.
|
|
58
|
-
"rollup": "^4.
|
|
57
|
+
"npm-check-updates": "^16.14.15",
|
|
58
|
+
"rollup": "^4.10.0",
|
|
59
59
|
"rollup-plugin-dts": "^6.1.0",
|
|
60
|
-
"ts-jest": "^29.1.
|
|
61
|
-
"ts-node": "^10.9.
|
|
62
|
-
"tsx": "^4.
|
|
63
|
-
"typescript": "^5.3.
|
|
60
|
+
"ts-jest": "^29.1.2",
|
|
61
|
+
"ts-node": "^10.9.2",
|
|
62
|
+
"tsx": "^4.7.1",
|
|
63
|
+
"typescript": "^5.3.3"
|
|
64
64
|
}
|
|
65
65
|
}
|