From 97b76e9d9d417a84530ff601ed2e75220be27573 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Wed, 17 Jul 2024 19:31:18 +0200 Subject: [PATCH] Use ES modules, add types and github action checks --- .circleci/config.yml | 37 ------------------------------- .github/workflows/pullrequest.yml | 21 ++++++++++++++++++ .gitignore | 1 + index.d.ts.map | 1 + index.js | 10 +++++++-- package-lock.json | 22 +++++++++++++++++- package.json | 12 ++++++---- test/index.test.js | 4 ++-- tsconfig.build.json | 11 +++++++++ 9 files changed, 73 insertions(+), 46 deletions(-) delete mode 100644 .circleci/config.yml create mode 100644 .github/workflows/pullrequest.yml create mode 100644 index.d.ts.map create mode 100644 tsconfig.build.json diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index a5409bb..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,37 +0,0 @@ -# Javascript Node CircleCI 2.0 configuration file -# -# Check https://circleci.com/docs/2.0/language-javascript/ for more details -# -version: 2 -jobs: - build: - docker: - # specify the version you desire here - - image: circleci/node:latest-browsers - - # Specify service dependencies here if necessary - # CircleCI maintains a library of pre-built images - # documented at https://circleci.com/docs/2.0/circleci-images/ - # - image: circleci/mongo:3.4.4 - - working_directory: ~/repo - - steps: - - checkout - - # Download and cache dependencies - - restore_cache: - keys: - - v1-dependencies-{{ checksum "package.json" }} - # fallback to using the latest cache if no exact match is found - - v1-dependencies- - - - run: npm install - - - save_cache: - paths: - - node_modules - key: v1-dependencies-{{ checksum "package.json" }} - - # run tests! - - run: npm test diff --git a/.github/workflows/pullrequest.yml b/.github/workflows/pullrequest.yml new file mode 100644 index 0000000..9f88aeb --- /dev/null +++ b/.github/workflows/pullrequest.yml @@ -0,0 +1,21 @@ +name: Node.js CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: '20.x' + - run: npm ci + - run: npm run build --if-present + - run: npm test \ No newline at end of file diff --git a/.gitignore b/.gitignore index 3c3629e..9502ea7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +**/*.d.ts diff --git a/index.d.ts.map b/index.d.ts.map new file mode 100644 index 0000000..91677d4 --- /dev/null +++ b/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AA+BA;;;;;GAKG;AACH,wCALW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,QACpB,MAAM,eACN,MAAM,GAAC,MAAM,GACX,MAAM,CAoDlB"} \ No newline at end of file diff --git a/index.js b/index.js index 7f440c7..9ee6cf2 100644 --- a/index.js +++ b/index.js @@ -29,7 +29,13 @@ var italicRE = /(italic|oblique)$/i; var fontCache = {}; -module.exports = function(fonts, size, lineHeight) { +/** + * @param {string|Array} fonts Mapbox GL Style fontstack or single font, e.g. `['Open Sans Regular', 'Arial Unicode MS Regular']` or `'Open Sans Regular'`. + * @param {number} size Font size in pixels. + * @param {string|number} [lineHeight] Line height as css line-height. + * @returns {string} CSS font definition, e.g. `'normal 400 16px/1.2 "Open Sans"'`. + */ +export default function(fonts, size, lineHeight) { var cssData = fontCache[fonts]; if (!cssData) { if (!Array.isArray(fonts)) { @@ -79,4 +85,4 @@ module.exports = function(fonts, size, lineHeight) { cssData = fontCache[fonts] = [style, weight, fontFamilies]; } return cssData[0] + sp + cssData[1] + sp + size + 'px' + (lineHeight ? '/' + lineHeight : '') + sp + cssData[2]; -}; +} diff --git a/package-lock.json b/package-lock.json index 2e897ca..c48f850 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,8 @@ "karma-mocha": "^2.0.1", "mocha": "^10.2.0", "should": "^13.2.3", - "sinon": "^7.2.7" + "sinon": "^7.2.7", + "typescript": "^5.5.3" } }, "node_modules/@babel/code-frame": { @@ -2778,6 +2779,19 @@ "node": ">=4" } }, + "node_modules/typescript": { + "version": "5.5.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.3.tgz", + "integrity": "sha512-/hreyEujaB0w76zKo6717l3L0o/qEUtRgdvUBvlkhoWeOVMjMuHNHk0BRBzikzuGDqNmPQbg5ifMEqsHLiIUcQ==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/unbox-primitive": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.1.tgz", @@ -5192,6 +5206,12 @@ "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==", "dev": true }, + "typescript": { + "version": "5.5.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.3.tgz", + "integrity": "sha512-/hreyEujaB0w76zKo6717l3L0o/qEUtRgdvUBvlkhoWeOVMjMuHNHk0BRBzikzuGDqNmPQbg5ifMEqsHLiIUcQ==", + "dev": true + }, "unbox-primitive": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.1.tgz", diff --git a/package.json b/package.json index 90d487e..def27ac 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,10 @@ { "name": "mapbox-to-css-font", - "version": "2.4.5", + "version": "3.0.0", "description": "Utility to convert Mapbox GL Style font names to CSS font definitions", - "main": "index.js", + "type": "module", + "module": "index.js", + "types": "index.d.ts", "repository": { "type": "git", "url": "git://github.com/openlayers/mapbox-to-css-font.git" @@ -16,7 +18,8 @@ "scripts": { "lint": "eslint *.js test", "pretest": "npm run lint", - "test": "mocha test/index.test.js" + "test": "mocha test/index.test.js && tsc -p tsconfig.build.json --noEmit", + "prepare": "tsc --emitDeclarationOnly -p tsconfig.build.json" }, "devDependencies": { "eslint": "^5.15.0", @@ -24,6 +27,7 @@ "karma-mocha": "^2.0.1", "mocha": "^10.2.0", "should": "^13.2.3", - "sinon": "^7.2.7" + "sinon": "^7.2.7", + "typescript": "^5.5.3" } } diff --git a/test/index.test.js b/test/index.test.js index 6f2d36a..b29aa3d 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,6 +1,6 @@ /* eslint-env mocha */ -const should = require('should'); -const parseFont = require('../index.js'); +import should from 'should'; +import parseFont from '../index.js'; describe('mapbox-to-css-font', function() { diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 0000000..f7ed03f --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "allowJs": true, + "checkJs": true, + "lib": ["es2015"], + "declaration": true, + "declarationMap": true + }, + "include": ["**/*.js"], + "exclude": ["node_modules", "test"] +} \ No newline at end of file