diff --git a/src/index.test.ts b/src/index.test.ts index 4a7f2b0..8a74f6c 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,31 +1,26 @@ -import { Colorful } from './index.js'; +/* eslint-disable @typescript-eslint/naming-convention */ import * as builderClasses from './index.js'; import StyleBuilder from './lib/style_builder.js'; describe('Style Builders', () => { - const styleNames = [ - 'Colorful', - 'Graybeard', - 'Neutrino', + const styles = [ + { name: 'Colorful', builderClass: builderClasses.Colorful }, + { name: 'Graybeard', builderClass: builderClasses.Graybeard }, + { name: 'Neutrino', builderClass: builderClasses.Neutrino }, ]; - it(`should have the correct ${styleNames.length} styles`, () => { - const keys1 = Array.from(Object.keys(builderClasses)).sort(); - const keys2 = styleNames.sort(); - expect(keys1).toEqual(keys2); - }); - - Object.entries(builderClasses).forEach(([styleName, builderClass]) => { - it(`should create and test an instance of ${styleName}`, () => { + styles.forEach(({ name, builderClass }) => { + it(`should create and test an instance of ${name}`, () => { const builder = new builderClass(); expect(builder).toBeInstanceOf(StyleBuilder); expect(typeof builder.name).toBe('string'); + expect(builder.name).toBe(name); builder.baseUrl = 'https://example.org'; const style = builder.build(); expect(JSON.stringify(style).length).toBeGreaterThan(50000); - expect(style.name).toBe('versatiles-' + styleName.toLowerCase()); + expect(style.name).toBe('versatiles-' + name.toLowerCase()); expect(style.glyphs).toBe('https://example.org/assets/fonts/{fontstack}/{range}.pbf'); expect(style.sprite).toBe('https://example.org/assets/sprites/sprites'); expect(Object.keys(style.sources).join(',')).toBe('versatiles-shortbread'); @@ -36,9 +31,39 @@ describe('Style Builders', () => { }); describe('Colorful', () => { - const colorful = new Colorful(); + const colorful = new builderClasses.Colorful(); colorful.baseUrl = 'https://dev.null'; colorful.defaultColors.commercial = '#f00'; const style = colorful.build(); expect(style.glyphs).toBe('https://dev.null/assets/fonts/{fontstack}/{range}.pbf'); }); + +describe('guessStyle', () => { + it('should build raster styles', () => { + const style = builderClasses.guessStyle({ + type: 'raster', + tiles: [], + format: 'avif', + }); + expect(style).toStrictEqual({ + layers: [{ id: 'raster', source: 'rasterSource', type: 'raster' }], + sources: { rasterSource: { format: 'avif', tilejson: '3.0.0', tiles: [], type: 'raster' } }, + version: 8, + }); + }); + + + it('should build vector styles', () => { + const style = builderClasses.guessStyle({ + type: 'vector', + tiles: [], + format: 'pbf', + vector_layers: [], + }); + expect(style).toStrictEqual({ + layers: [{ id: 'background', paint: { 'background-color': '#fff' }, type: 'background' }], + sources: { vectorSource: { format: 'pbf', tilejson: '3.0.0', tiles: [], type: 'vector', 'vector_layers': [] } }, + 'version': 8, + }); + }); +}); diff --git a/src/index.ts b/src/index.ts index de0dec8..3a47145 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,8 @@ export type { MaplibreStyle } from './lib/types.js'; +export { default as guessStyle } from './lib/style_guesser.js'; + export { default as Colorful } from './style/colorful.js'; export { default as Graybeard } from './style/graybeard.js'; export { default as Neutrino } from './style/neutrino.js'; diff --git a/src/lib/style_builder.ts b/src/lib/style_builder.ts index cb9da91..48d7a7a 100644 --- a/src/lib/style_builder.ts +++ b/src/lib/style_builder.ts @@ -106,7 +106,7 @@ export default abstract class StyleBuilder l.type !== 'symbol'); style.layers = layers; - style.name = 'versatiles-' + this.name; + style.name = 'versatiles-' + this.name.toLowerCase(); style.glyphs = resolveUrl(this.baseUrl, this.glyphsUrl); style.sprite = resolveUrl(this.baseUrl, this.spriteUrl); diff --git a/src/style/colorful.ts b/src/style/colorful.ts index 7790445..a0a7f73 100644 --- a/src/style/colorful.ts +++ b/src/style/colorful.ts @@ -4,7 +4,7 @@ import StyleBuilder from '../lib/style_builder.js'; import type { StyleRules, StyleRulesOptions } from '../lib/types.js'; export default class Colorful extends StyleBuilder { - public readonly name: string = 'colorful'; + public readonly name: string = 'Colorful'; public defaultFonts = { regular: 'noto_sans_regular', diff --git a/src/style/graybeard.ts b/src/style/graybeard.ts index d96c24e..2eed574 100644 --- a/src/style/graybeard.ts +++ b/src/style/graybeard.ts @@ -1,7 +1,7 @@ import Colorful from './colorful.js'; export default class Graybeard extends Colorful { - public readonly name: string = 'graybeard'; + public readonly name: string = 'Graybeard'; public constructor() { super(); diff --git a/src/style/neutrino.ts b/src/style/neutrino.ts index b4a7b32..b9d6b88 100644 --- a/src/style/neutrino.ts +++ b/src/style/neutrino.ts @@ -4,7 +4,7 @@ import StyleBuilder from '../lib/style_builder.js'; import type { StyleRules, StyleRulesOptions } from '../lib/types.js'; export default class Neutrino extends StyleBuilder { - public readonly name: string = 'neutrino'; + public readonly name: string = 'Neutrino'; public defaultFonts = { regular: 'noto_sans_regular',