@versatiles/style 3.5.1 → 3.6.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.
@@ -0,0 +1,86 @@
1
+ import type { BackgroundLayerSpecification, FillLayerSpecification, FilterSpecification, LineLayerSpecification, StyleSpecification, SymbolLayerSpecification } from '@maplibre/maplibre-gl-style-spec';
2
+ import type Color from 'color';
3
+ import type StyleBuilder from './style_builder.ts';
4
+ export type TileFormat = 'avif' | 'bin' | 'geojson' | 'jpg' | 'json' | 'pbf' | 'png' | 'svg' | 'topojson' | 'webp';
5
+ export type MaplibreLayer = BackgroundLayerSpecification | FillLayerSpecification | LineLayerSpecification | SymbolLayerSpecification;
6
+ export type MaplibreLayerDefinition = BackgroundLayerSpecification | Omit<FillLayerSpecification, 'source'> | Omit<LineLayerSpecification, 'source'> | Omit<SymbolLayerSpecification, 'source'>;
7
+ export type MaplibreFilter = FilterSpecification;
8
+ export interface VectorLayer {
9
+ id: string;
10
+ fields: Record<string, 'Boolean' | 'Number' | 'String'>;
11
+ description?: string;
12
+ minzoom?: number;
13
+ maxzoom?: number;
14
+ }
15
+ export interface TileJSONSpecificationRaster {
16
+ tilejson?: '3.0.0';
17
+ type: 'raster';
18
+ attribution?: string;
19
+ tiles: string[];
20
+ scheme?: 'tms' | 'xyz';
21
+ bounds?: [number, number, number, number];
22
+ center?: [number, number];
23
+ description?: string;
24
+ fillzoom?: number;
25
+ grids?: string[];
26
+ legend?: string;
27
+ minzoom?: number;
28
+ maxzoom?: number;
29
+ name?: string;
30
+ template?: string;
31
+ format: 'avif' | 'jpg' | 'png' | 'webp';
32
+ }
33
+ export interface TileJSONSpecificationVector extends Omit<TileJSONSpecificationRaster, 'format' | 'type'> {
34
+ vector_layers: VectorLayer[];
35
+ format: 'pbf';
36
+ type: 'vector';
37
+ }
38
+ export type TileJSONSpecification = TileJSONSpecificationRaster | TileJSONSpecificationVector;
39
+ export type MaplibreStyleRaster = Omit<StyleSpecification, 'sources'> & {
40
+ 'sources': {
41
+ [_: string]: TileJSONSpecificationRaster;
42
+ };
43
+ };
44
+ export type MaplibreStyleVector = Omit<StyleSpecification, 'sources'> & {
45
+ 'sources': {
46
+ [_: string]: TileJSONSpecificationVector;
47
+ };
48
+ };
49
+ export type MaplibreStyle = MaplibreStyleRaster | MaplibreStyleVector;
50
+ export type StyleRuleValue = boolean | number | object | string;
51
+ export type StyleRule = Record<string, StyleRuleValue | undefined>;
52
+ export type StyleRules = Record<string, StyleRule | undefined>;
53
+ export type LanguageSuffix = '_de' | '_en' | '';
54
+ export type StylemakerColorKeys<T extends StyleBuilder<T>> = keyof T['defaultColors'];
55
+ export type StylemakerFontKeys<T extends StyleBuilder<T>> = keyof T['defaultFonts'];
56
+ export type StylemakerColorStrings<T extends StyleBuilder<T>> = Record<StylemakerColorKeys<T>, string>;
57
+ export type StylemakerFontStrings<T extends StyleBuilder<T>> = Record<StylemakerFontKeys<T>, string>;
58
+ export type StylemakerColors<T extends StyleBuilder<T>> = Record<StylemakerColorKeys<T>, Color>;
59
+ export type StylemakerFonts<T extends StyleBuilder<T>> = Record<StylemakerFontKeys<T>, string>;
60
+ export interface StyleRulesOptions<T extends StyleBuilder<T>> {
61
+ colors: StylemakerColors<T>;
62
+ fonts: StylemakerFontStrings<T>;
63
+ languageSuffix: string;
64
+ }
65
+ export interface RecolorOptions {
66
+ invert?: boolean;
67
+ rotate?: number;
68
+ saturate?: number;
69
+ gamma?: number;
70
+ contrast?: number;
71
+ brightness?: number;
72
+ tint?: number;
73
+ tintColor?: string;
74
+ }
75
+ export interface StylemakerOptions<T extends StyleBuilder<T>> {
76
+ baseUrl?: string;
77
+ glyphsUrl?: string;
78
+ spriteUrl?: string;
79
+ tilesUrls?: string[];
80
+ hideLabels?: boolean;
81
+ languageSuffix?: LanguageSuffix;
82
+ colors?: Partial<StylemakerColorStrings<T>>;
83
+ fonts?: Partial<StylemakerFontStrings<T>>;
84
+ recolor?: RecolorOptions;
85
+ }
86
+ export declare function isTileJSONSpecification(obj: unknown): obj is TileJSONSpecification;
@@ -0,0 +1,89 @@
1
+ /* eslint-disable @typescript-eslint/no-unnecessary-condition */
2
+ export function isTileJSONSpecification(obj) {
3
+ if (typeof obj !== 'object' || obj === null) {
4
+ throw Error('spec must be an object');
5
+ }
6
+ const spec = obj;
7
+ // Common property validation
8
+ if (typeof spec.tilejson !== 'undefined' && spec.tilejson !== '3.0.0') {
9
+ throw Error('spec.tilejson must be "3.0.0" if present');
10
+ }
11
+ if (typeof spec.attribution !== 'undefined' && typeof spec.attribution !== 'string') {
12
+ throw Error('spec.attribution must be a string if present');
13
+ }
14
+ if (typeof spec.scheme !== 'undefined' && !['tms', 'xyz'].includes(spec.scheme)) {
15
+ throw Error('spec.scheme must be "tms" or "xyz" if present');
16
+ }
17
+ if (typeof spec.bounds !== 'undefined' && (!Array.isArray(spec.bounds) || spec.bounds.length !== 4 || spec.bounds.some(num => typeof num !== 'number'))) {
18
+ throw Error('spec.bounds must be an array of four numbers if present');
19
+ }
20
+ if (typeof spec.center !== 'undefined' && (!Array.isArray(spec.center) || spec.center.length !== 2 || spec.center.some(num => typeof num !== 'number'))) {
21
+ throw Error('spec.center must be an array of two numbers if present');
22
+ }
23
+ if (typeof spec.description !== 'undefined' && typeof spec.description !== 'string') {
24
+ throw Error('spec.description must be a string if present');
25
+ }
26
+ if (typeof spec.fillzoom !== 'undefined' && typeof spec.fillzoom !== 'number') {
27
+ throw Error('spec.fillzoom must be a number if present');
28
+ }
29
+ if (typeof spec.grids !== 'undefined' && (!Array.isArray(spec.grids) || spec.grids.some(url => typeof url !== 'string'))) {
30
+ throw Error('spec.grids must be an array of strings if present');
31
+ }
32
+ if (typeof spec.legend !== 'undefined' && typeof spec.legend !== 'string') {
33
+ throw Error('spec.legend must be a string if present');
34
+ }
35
+ if (typeof spec.minzoom !== 'undefined' && typeof spec.minzoom !== 'number') {
36
+ throw Error('spec.minzoom must be a number if present');
37
+ }
38
+ if (typeof spec.maxzoom !== 'undefined' && typeof spec.maxzoom !== 'number') {
39
+ throw Error('spec.maxzoom must be a number if present');
40
+ }
41
+ if (typeof spec.name !== 'undefined' && typeof spec.name !== 'string') {
42
+ throw Error('spec.name must be a string if present');
43
+ }
44
+ if (typeof spec.template !== 'undefined' && typeof spec.template !== 'string') {
45
+ throw Error('spec.template must be a string if present');
46
+ }
47
+ if (spec.type === 'raster') {
48
+ if (!['avif', 'jpg', 'png', 'webp'].includes(spec.format)) {
49
+ throw Error('spec.format must be "avif", "jpg", "png", or "webp"');
50
+ }
51
+ if (!Array.isArray(spec.tiles) || spec.tiles.some(url => typeof url !== 'string')) {
52
+ throw Error('spec.tiles must be an array of strings');
53
+ }
54
+ }
55
+ else if (spec.type === 'vector') {
56
+ if (spec.format !== 'pbf') {
57
+ throw Error('spec.format must be "pbf"');
58
+ }
59
+ if (!Array.isArray(spec.vector_layers) || spec.vector_layers.some(layer => !validateVectorLayer(layer))) {
60
+ throw Error('spec.vector_layers must be an array of VectorLayer');
61
+ }
62
+ }
63
+ else {
64
+ throw Error('spec.type must be "raster" or "vector"');
65
+ }
66
+ return true;
67
+ }
68
+ function validateVectorLayer(obj) {
69
+ if (typeof obj !== 'object' || obj === null) {
70
+ throw Error('layer must be an object');
71
+ }
72
+ const layer = obj;
73
+ if (typeof layer.id !== 'string') {
74
+ throw Error('layer.id must be a string');
75
+ }
76
+ if (typeof layer.fields !== 'object' || layer.fields === null || Object.values(layer.fields).some(type => !['Boolean', 'Number', 'String'].includes(type))) {
77
+ throw Error('layer.fields must be an object with values "Boolean", "Number", or "String"');
78
+ }
79
+ if (typeof layer.description !== 'undefined' && typeof layer.description !== 'string') {
80
+ throw Error('layer.description must be a string if present');
81
+ }
82
+ if (typeof layer.minzoom !== 'undefined' && (typeof layer.minzoom !== 'number' || layer.minzoom < 0)) {
83
+ throw Error('layer.minzoom must be a non-negative number if present');
84
+ }
85
+ if (typeof layer.maxzoom !== 'undefined' && (typeof layer.maxzoom !== 'number' || layer.maxzoom < 0)) {
86
+ throw Error('layer.maxzoom must be a non-negative number if present');
87
+ }
88
+ return true;
89
+ }
@@ -1,12 +1,12 @@
1
1
  import StyleBuilder from '../lib/style_builder.js';
2
- import type { StyleRules, StyleRulesOptions } from '../lib/style_builder.js';
3
- export default class Colorful extends StyleBuilder {
2
+ import type { StyleRules, StyleRulesOptions } from '../lib/types.js';
3
+ export default class Colorful extends StyleBuilder<Colorful> {
4
4
  readonly name: string;
5
- fonts: {
5
+ defaultFonts: {
6
6
  regular: string;
7
7
  bold: string;
8
8
  };
9
- colors: {
9
+ defaultColors: {
10
10
  land: string;
11
11
  water: string;
12
12
  glacier: string;
@@ -49,5 +49,5 @@ export default class Colorful extends StyleBuilder {
49
49
  hospital: string;
50
50
  poi: string;
51
51
  };
52
- protected getStyleRules(options: StyleRulesOptions): StyleRules;
52
+ protected getStyleRules(options: StyleRulesOptions<Colorful>): StyleRules;
53
53
  }
@@ -1,12 +1,12 @@
1
1
  /* eslint-disable @typescript-eslint/naming-convention */
2
2
  import StyleBuilder from '../lib/style_builder.js';
3
3
  export default class Colorful extends StyleBuilder {
4
- name = 'colorful';
5
- fonts = {
4
+ name = 'Colorful';
5
+ defaultFonts = {
6
6
  regular: 'noto_sans_regular',
7
7
  bold: 'noto_sans_bold',
8
8
  };
9
- colors = {
9
+ defaultColors = {
10
10
  land: '#F9F4EE',
11
11
  water: '#BEDDF3',
12
12
  glacier: '#FFFFFF',
@@ -1,8 +1,8 @@
1
1
  import Colorful from './colorful.js';
2
2
  export default class Graybeard extends Colorful {
3
- name = 'graybeard';
3
+ name = 'Graybeard';
4
4
  constructor() {
5
5
  super();
6
- this.resetColors(color => color.desaturate(1));
6
+ this.transformDefaultColors(color => color.desaturate(1));
7
7
  }
8
8
  }
@@ -1,12 +1,12 @@
1
1
  import StyleBuilder from '../lib/style_builder.js';
2
- import type { StyleRules, StyleRulesOptions } from '../lib/style_builder.js';
3
- export default class Neutrino extends StyleBuilder {
2
+ import type { StyleRules, StyleRulesOptions } from '../lib/types.js';
3
+ export default class Neutrino extends StyleBuilder<Neutrino> {
4
4
  readonly name: string;
5
- fonts: {
5
+ defaultFonts: {
6
6
  regular: string;
7
7
  bold: string;
8
8
  };
9
- colors: {
9
+ defaultColors: {
10
10
  land: string;
11
11
  water: string;
12
12
  grass: string;
@@ -20,5 +20,5 @@ export default class Neutrino extends StyleBuilder {
20
20
  rail: string;
21
21
  label: string;
22
22
  };
23
- protected getStyleRules(options: StyleRulesOptions): StyleRules;
23
+ protected getStyleRules(options: StyleRulesOptions<Neutrino>): StyleRules;
24
24
  }
@@ -1,12 +1,12 @@
1
1
  /* eslint-disable @typescript-eslint/naming-convention */
2
2
  import StyleBuilder from '../lib/style_builder.js';
3
3
  export default class Neutrino extends StyleBuilder {
4
- name = 'neutrino';
5
- fonts = {
4
+ name = 'Neutrino';
5
+ defaultFonts = {
6
6
  regular: 'noto_sans_regular',
7
7
  bold: 'noto_sans_bold',
8
8
  };
9
- colors = {
9
+ defaultColors = {
10
10
  land: '#f6f0f6',
11
11
  water: '#cbd2df',
12
12
  grass: '#e7e9e5',
package/package.json CHANGED
@@ -1,21 +1,23 @@
1
1
  {
2
2
  "name": "@versatiles/style",
3
- "version": "3.5.1",
3
+ "version": "3.6.0",
4
4
  "description": "Generate StyleJSON for MapLibre",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
- "check": "npm run lint && npm run build && npm run test",
8
+ "check": "npm run build && npm run lint && npm run test",
9
9
  "build": "npm run build-browser && npm run build-node && npm run build-styles",
10
10
  "build-browser": "rollup -c=rollup.config.js",
11
- "build-node": "rm -rf dist && tsc -p tsconfig.node.json",
11
+ "build-node": "rm -rf dist && tsc -p tsconfig.node.json && chmod +x dist/index.js",
12
12
  "build-styles": "tsx scripts/build-styles.ts",
13
- "lint": "eslint .",
13
+ "doc": "vrt ts2md src/index.ts tsconfig.node.json | vrt insertmd README.md '# API'",
14
+ "lint": "npm run build-browser && eslint --color .",
14
15
  "release": "tsx scripts/release.ts",
15
- "test": "npm run test-node && npm run test-coverage && npm run test-browser",
16
- "test-browser": "npm run build-browser && echo 'add browser test'",
16
+ "test": "npm run test-typescript && npm run test-node && npm run test-browser",
17
+ "test-browser": "npm run build-browser && jest -c=jest.config.browser.ts",
17
18
  "test-coverage": "NODE_OPTIONS=--experimental-vm-modules jest -c=jest.config.coverage.ts",
18
19
  "test-node": "npm run build-node && NODE_OPTIONS=--experimental-vm-modules jest -c=jest.config.node.ts",
20
+ "test-typescript": "NODE_OPTIONS=--experimental-vm-modules jest -c=jest.config.typescript.ts",
19
21
  "upgrade": "npm-check-updates -u && rm -f package-lock.json && rm -rf node_modules; npm i"
20
22
  },
21
23
  "repository": {
@@ -27,7 +29,6 @@
27
29
  "type": "module",
28
30
  "dependencies": {
29
31
  "@types/color": "^3.0.6",
30
- "@types/mapbox-gl": "^2.7.18",
31
32
  "brace-expansion": "^3.0.0",
32
33
  "color": "^4.2.3"
33
34
  },
@@ -43,20 +44,22 @@
43
44
  "@rollup/plugin-typescript": "^11.1.5",
44
45
  "@types/brace-expansion": "^1.1.2",
45
46
  "@types/inquirer": "^9.0.7",
46
- "@types/jest": "^29.5.8",
47
- "@types/node": "^20.9.0",
48
- "@typescript-eslint/eslint-plugin": "^6.10.0",
49
- "@typescript-eslint/parser": "^6.10.0",
50
- "eslint": "^8.53.0",
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",
51
+ "@versatiles/release-tool": "^1.0.3",
52
+ "eslint": "^8.54.0",
51
53
  "inquirer": "^9.2.12",
52
54
  "jest": "^29.7.0",
55
+ "jest-environment-jsdom": "^29.7.0",
53
56
  "jest-ts-webcompat-resolver": "^1.0.0",
54
- "npm-check-updates": "^16.14.6",
55
- "rollup": "^4.3.1",
57
+ "npm-check-updates": "^16.14.11",
58
+ "rollup": "^4.6.0",
56
59
  "rollup-plugin-dts": "^6.1.0",
57
60
  "ts-jest": "^29.1.1",
58
61
  "ts-node": "^10.9.1",
59
- "tsx": "^4.1.1",
60
- "typescript": "^5.2.2"
62
+ "tsx": "^4.5.0",
63
+ "typescript": "^5.3.2"
61
64
  }
62
65
  }