@versatiles/style 3.7.0 → 3.8.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.
package/README.MD CHANGED
@@ -125,7 +125,7 @@ interface {
125
125
 
126
126
  ### Method: `colorful(options)`
127
127
 
128
- <sup><a href="https://github.com/versatiles-org/versatiles-style/blob/c839905/src/index.ts#L4">\[src]</a></sup>
128
+ <sup><a href="https://github.com/versatiles-org/versatiles-style/blob/be6dcfe/src/index.ts#L4">\[src]</a></sup>
129
129
 
130
130
  **Parameters:**
131
131
 
@@ -135,7 +135,7 @@ interface {
135
135
 
136
136
  ### Method: `graybeard(options)`
137
137
 
138
- <sup><a href="https://github.com/versatiles-org/versatiles-style/blob/c839905/src/index.ts#L9">\[src]</a></sup>
138
+ <sup><a href="https://github.com/versatiles-org/versatiles-style/blob/be6dcfe/src/index.ts#L9">\[src]</a></sup>
139
139
 
140
140
  **Parameters:**
141
141
 
@@ -145,7 +145,7 @@ interface {
145
145
 
146
146
  ### Method: `guessStyle(opt)`
147
147
 
148
- <sup><a href="https://github.com/versatiles-org/versatiles-style/blob/c839905/src/lib/style_guesser.ts#L12">\[src]</a></sup>
148
+ <sup><a href="https://github.com/versatiles-org/versatiles-style/blob/be6dcfe/src/lib/style_guesser.ts#L13">\[src]</a></sup>
149
149
 
150
150
  **Parameters:**
151
151
 
@@ -155,7 +155,7 @@ interface {
155
155
 
156
156
  ### Method: `neutrino(options)`
157
157
 
158
- <sup><a href="https://github.com/versatiles-org/versatiles-style/blob/c839905/src/index.ts#L14">\[src]</a></sup>
158
+ <sup><a href="https://github.com/versatiles-org/versatiles-style/blob/be6dcfe/src/index.ts#L14">\[src]</a></sup>
159
159
 
160
160
  **Parameters:**
161
161
 
@@ -12,6 +12,7 @@ export default function getTemplate() {
12
12
  sprite: 'https://tiles.versatiles.org/sprites/sprites',
13
13
  sources: {
14
14
  'versatiles-shortbread': {
15
+ tilejson: '3.0.0',
15
16
  attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
16
17
  tiles: [
17
18
  'https://tiles.versatiles.org/tiles/osm/{z}/{x}/{y}',
@@ -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
@@ -45,17 +50,29 @@ export default function guess(opt) {
45
50
  }
46
51
  if (!isTileJSONSpecification(tilejson))
47
52
  throw Error();
53
+ let style;
48
54
  switch (tilejson.type) {
49
55
  case 'raster':
50
- return getImageStyle(tilejson);
56
+ style = getImageStyle(tilejson);
57
+ break;
51
58
  case 'vector':
52
59
  if (isShortbread(tilejson)) {
53
- return getShortbreadStyle(tilejson, { baseUrl: opt.baseUrl, glyphs: opt.glyphs, sprite: opt.sprite });
60
+ style = getShortbreadStyle(tilejson, opt);
54
61
  }
55
62
  else {
56
- return getInspectorStyle(tilejson);
63
+ style = getInspectorStyle(tilejson);
57
64
  }
58
65
  }
66
+ if (opt.minzoom ?? 0)
67
+ style.zoom ??= opt.minzoom;
68
+ if (opt.bounds)
69
+ style.center ??= [
70
+ (opt.bounds[0] + opt.bounds[2]) / 2,
71
+ (opt.bounds[1] + opt.bounds[3]) / 2,
72
+ ];
73
+ if (opt.center)
74
+ style.center = opt.center;
75
+ return style;
59
76
  }
60
77
  function isShortbread(spec) {
61
78
  if (typeof spec !== 'object')
@@ -69,14 +86,13 @@ function isShortbread(spec) {
69
86
  return shortbreadIds.every(id => layerIds.has(id));
70
87
  }
71
88
  function getShortbreadStyle(spec, builderOption) {
72
- const style = new Colorful().build({
89
+ return new Colorful().build({
73
90
  hideLabels: true,
74
91
  tiles: spec.tiles,
75
92
  baseUrl: builderOption.baseUrl,
76
93
  glyphs: builderOption.glyphs,
77
94
  sprite: builderOption.sprite,
78
95
  });
79
- return style;
80
96
  }
81
97
  function getInspectorStyle(spec) {
82
98
  const sourceName = 'vectorSource';
@@ -1,7 +1,10 @@
1
1
  /* eslint-disable @typescript-eslint/naming-convention */
2
+ import getTemplate from './shortbread/template.js';
2
3
  import guessStyle from './style_guesser.js';
3
4
  describe('guessStyle', () => {
4
5
  const tiles = ['https://example.com/tiles/{z}/{x}/{y}'];
6
+ const vectorLayersSomething = [{ id: 'geometry', fields: { label: 'String', height: 'Number' } }];
7
+ const vectorLayersShortbread = getTemplate().sources['versatiles-shortbread'].vector_layers;
5
8
  it('should build raster styles', () => {
6
9
  const type = 'raster';
7
10
  const format = 'avif';
@@ -12,14 +15,13 @@ describe('guessStyle', () => {
12
15
  layers: [{ id: 'raster', source: 'rasterSource', type }],
13
16
  });
14
17
  });
15
- it('should build vector styles', () => {
18
+ it('should build vector inspector styles', () => {
16
19
  const type = 'vector';
17
20
  const format = 'pbf';
18
- const vectorLayers = [{ id: 'geometry', fields: { label: 'String', height: 'Number' } }];
19
- expect(guessStyle({ tiles, format, vectorLayers }))
21
+ expect(guessStyle({ tiles, format, vectorLayers: vectorLayersSomething }))
20
22
  .toStrictEqual({
21
23
  version: 8,
22
- sources: { vectorSource: { format, tilejson: '3.0.0', tiles, type, vector_layers: vectorLayers } },
24
+ sources: { vectorSource: { format, tilejson: '3.0.0', tiles, type, vector_layers: vectorLayersSomething } },
23
25
  layers: [
24
26
  {
25
27
  id: 'background',
@@ -54,4 +56,69 @@ describe('guessStyle', () => {
54
56
  ],
55
57
  });
56
58
  });
59
+ it('should build shortbread vector styles', () => {
60
+ const type = 'vector';
61
+ const format = 'pbf';
62
+ const style = guessStyle({ tiles, format, vectorLayers: vectorLayersShortbread, baseUrl: 'http://example.com' });
63
+ expect(style.layers.length).toBe(236);
64
+ style.layers = [];
65
+ expect(style).toStrictEqual({
66
+ glyphs: 'http://example.com/assets/fonts/{fontstack}/{range}.pbf',
67
+ metadata: {
68
+ license: 'https://creativecommons.org/publicdomain/zero/1.0/',
69
+ 'maputnik:renderer': 'mbgljs',
70
+ },
71
+ name: 'versatiles-colorful',
72
+ sprite: 'http://example.com/assets/sprites/sprites',
73
+ layers: [],
74
+ sources: {
75
+ 'versatiles-shortbread': {
76
+ attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
77
+ bounds: [-180, -85.0511287798066, 180, 85.0511287798066],
78
+ format,
79
+ maxzoom: 14,
80
+ minzoom: 0,
81
+ scheme: 'xyz',
82
+ tilejson: '3.0.0',
83
+ tiles,
84
+ type,
85
+ vector_layers: vectorLayersShortbread,
86
+ },
87
+ },
88
+ version: 8,
89
+ });
90
+ });
91
+ const cases = [
92
+ { type: 'image', options: { tiles, format: 'png' } },
93
+ { type: 'inspector', options: { tiles, format: 'pbf', vectorLayers: vectorLayersSomething } },
94
+ { type: 'shortbread', options: { tiles, format: 'pbf', vectorLayers: vectorLayersShortbread } },
95
+ ];
96
+ describe('minzoom sets zoom', () => {
97
+ cases.forEach(({ type, options }) => {
98
+ it(type, () => {
99
+ expect(guessStyle({ ...options, minzoom: 5 })).toHaveProperty('zoom', 5);
100
+ });
101
+ });
102
+ });
103
+ describe('bounds sets center', () => {
104
+ cases.forEach(({ type, options }) => {
105
+ it(type, () => {
106
+ expect(guessStyle({ ...options, bounds: [1, 2, 3, 4] })).toHaveProperty('center', [2, 3]);
107
+ });
108
+ });
109
+ });
110
+ describe('center sets center', () => {
111
+ cases.forEach(({ type, options }) => {
112
+ it(type, () => {
113
+ expect(guessStyle({ ...options, center: [12, 34] })).toHaveProperty('center', [12, 34]);
114
+ });
115
+ });
116
+ });
117
+ describe('center overrides bounds', () => {
118
+ cases.forEach(({ type, options }) => {
119
+ it(type, () => {
120
+ expect(guessStyle({ ...options, bounds: [1, 2, 3, 4], center: [12, 34] })).toHaveProperty('center', [12, 34]);
121
+ });
122
+ });
123
+ });
57
124
  });
@@ -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
+ }
@@ -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('Utilities tests', () => {
4
- describe('deepClone', () => {
5
- it('clones primitive types correctly', () => {
6
- expect(deepClone(10)).toBe(10);
7
- expect(deepClone(true)).toBe(true);
8
- expect(deepClone('string')).toBe('string');
9
- });
10
- it('clones an array correctly', () => {
11
- const array = [1, 2, { a: 'b' }];
12
- const clonedArray = deepClone(array);
13
- expect(clonedArray).toEqual(array);
14
- expect(clonedArray).not.toBe(array);
15
- });
16
- it('clones a simple object correctly', () => {
17
- const obj = { a: 'b', c: 1, d: true };
18
- const clonedObj = deepClone(obj);
19
- expect(clonedObj).toEqual(obj);
20
- expect(clonedObj).not.toBe(obj);
21
- });
22
- it('clones a Color instance correctly', () => {
23
- const color = new Color('#FF5733');
24
- const clonedColor = deepClone(color);
25
- expect(clonedColor.hex()).toBe(color.hex());
26
- });
27
- it('throws an error for non-implemented types', () => {
28
- const func = () => true;
29
- expect(() => deepClone(func)).toThrow('Not implemented yet: "function" case');
30
- });
31
- });
32
- describe('isSimpleObject', () => {
33
- it('identifies simple objects correctly', () => {
34
- expect(isSimpleObject({ a: 1 })).toBe(true);
35
- });
36
- it('rejects non-objects', () => {
37
- expect(isSimpleObject(1)).toBe(false);
38
- expect(isSimpleObject('a')).toBe(false);
39
- expect(isSimpleObject(true)).toBe(false);
40
- });
41
- it('rejects arrays', () => {
42
- expect(isSimpleObject([1, 2, 3])).toBe(false);
43
- });
44
- it('rejects objects with prototype properties', () => {
45
- class MyClass {
46
- #property = true;
47
- }
48
- expect(isSimpleObject(new MyClass())).toBe(false);
49
- });
50
- });
51
- describe('isBasicType', () => {
52
- it('returns true for basic types', () => {
53
- expect(isBasicType(1)).toBe(true);
54
- expect(isBasicType('string')).toBe(true);
55
- expect(isBasicType(true)).toBe(true);
56
- expect(isBasicType(undefined)).toBe(true);
57
- });
58
- it('returns false for objects', () => {
59
- expect(isBasicType({})).toBe(false);
60
- expect(isBasicType([])).toBe(false);
61
- });
62
- it('throws error for functions', () => {
63
- expect(() => isBasicType(() => true)).toThrow();
64
- });
65
- });
66
- describe('deepMerge', () => {
67
- it('merges simple objects correctly', () => {
68
- const target = { a: 1, b: 2 };
69
- const source = { b: 3, c: 4 };
70
- const result = deepMerge(target, source);
71
- expect(result).toEqual({ a: 1, b: 3, c: 4 });
72
- });
73
- it('merges nested objects correctly', () => {
74
- const target = { a: { d: 4 }, b: 2 };
75
- const source = { a: { e: 5 }, b: 3 };
76
- const result = deepMerge(target, source);
77
- expect(result).toEqual({ a: { d: 4, e: 5 }, b: 3 });
78
- });
79
- it('overrides primitives with object types', () => {
80
- const target = { a: 1, b: 2 };
81
- const source = { a: { d: 4 }, b: { e: 5 } };
82
- const result = deepMerge(target, source);
83
- expect(result).toEqual({ a: { d: 4 }, b: { e: 5 } });
84
- });
85
- it('merges Color instances correctly', () => {
86
- const target = { color: new Color('#FF5733') };
87
- const source = { color: new Color('#33FF57') };
88
- const result = deepMerge(target, source);
89
- expect(result.color.hex()).toBe(source.color.hex());
90
- });
91
- it('throws error for unpredicted cases', () => {
92
- const target = { a: () => false };
93
- const source = { a: { b: 1 } };
94
- expect(() => deepMerge(target, source)).toThrow('Not implemented yet: "function" case');
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.7.0",
3
+ "version": "3.8.1",
4
4
  "description": "Generate StyleJSON for MapLibre",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -44,22 +44,22 @@
44
44
  "@rollup/plugin-typescript": "^11.1.5",
45
45
  "@types/brace-expansion": "^1.1.2",
46
46
  "@types/inquirer": "^9.0.7",
47
- "@types/jest": "^29.5.10",
48
- "@types/node": "^20.10.0",
49
- "@typescript-eslint/eslint-plugin": "^6.13.0",
50
- "@typescript-eslint/parser": "^6.13.0",
47
+ "@types/jest": "^29.5.11",
48
+ "@types/node": "^20.10.6",
49
+ "@typescript-eslint/eslint-plugin": "^6.17.0",
50
+ "@typescript-eslint/parser": "^6.17.0",
51
51
  "@versatiles/release-tool": "^1.0.3",
52
- "eslint": "^8.54.0",
52
+ "eslint": "^8.56.0",
53
53
  "inquirer": "^9.2.12",
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.11",
58
- "rollup": "^4.6.0",
57
+ "npm-check-updates": "^16.14.12",
58
+ "rollup": "^4.9.3",
59
59
  "rollup-plugin-dts": "^6.1.0",
60
60
  "ts-jest": "^29.1.1",
61
- "ts-node": "^10.9.1",
62
- "tsx": "^4.5.0",
63
- "typescript": "^5.3.2"
61
+ "ts-node": "^10.9.2",
62
+ "tsx": "^4.7.0",
63
+ "typescript": "^5.3.3"
64
64
  }
65
65
  }