diff --git a/.github/workflows/translate-tests.yml b/.github/workflows/translate-tests.yml new file mode 100644 index 00000000..a261185b --- /dev/null +++ b/.github/workflows/translate-tests.yml @@ -0,0 +1,32 @@ +name: "Test Suite: @nodevu/translate" + +on: + pull_request: + paths: + - 'translate/**' + branches: + - main + workflow_dispatch: + +jobs: + tests: + if: github.repository == 'cutenode/nodevu' + runs-on: ubuntu-latest + strategy: + matrix: + node-version: [current, lts/*, lts/-1] + steps: + - name: Checkout the repository + uses: actions/checkout@v3 + - name: Setup Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + - name: Install latest npm + run: npm i -g npm + - name: Run npm install (project) + run: npm install + - name: Run npm install (package) + run: npm install -w translate + - name: Run npm test + run: npm test -w translate diff --git a/package.json b/package.json index a3e91dc0..d1221a90 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "parsefiles", "earliest", "ranges", - "aliases" + "aliases", + "translate" ] } diff --git a/translate/README.md b/translate/README.md new file mode 100644 index 00000000..2e59c147 --- /dev/null +++ b/translate/README.md @@ -0,0 +1,55 @@ +# @nodevu/translate + +A translation layer to use the terms previously defined by the Node.js Package Maintenance WG's [Package Support document](https://github.com/nodejs/package-maintenance/blob/main/docs/PACKAGE-SUPPORT.md) to terms that can easily be used by [@nodevu/ranges](https://npm.im/@nodevu/ranges). + + +## Usage + +```js +const ranges = require('@nodevu/ranges') +const translate = require('@nodevu/translate') + +async function getTranslatedData () { + // translation for the 'current' Package Support term + const current = await ranges(await translate('current')) + + // translation for the 'lts_latest' Package Support term + const lts_latest = await ranges(await translate('lts_latest')) + + // translation for the 'lts' Package Support term + const lts = await ranges(await translate('lts')) + + // translation for the 'supported' Package Support term + const supported = await ranges(await translate('supported')) + + // translation for the 'all' Package Support term + const all = await ranges(await translate('all')) + + // return the data in a single object + return { + current, + lts_latest, + lts, + supported, + all + } +} + +getTranslatedData().then(console.log) +``` + +## API + +- `translate(alias)` + - `alias` (string): A Node.js Package Support Alias. + +Possible values for `alias`: + - `current`: returns `['current']` + - `lts_latest`: returns `['lts/latest']` + - `lts`: returns `['lts/active', 'lts/maintenance']` + - `supported`: returns `['current', 'lts/active', 'lts/maintenance']` + - `all`: returns `['current', 'lts/active', 'lts/maintenance', 'eol']` + + + + diff --git a/translate/examples/default.js b/translate/examples/default.js new file mode 100644 index 00000000..ba079325 --- /dev/null +++ b/translate/examples/default.js @@ -0,0 +1,30 @@ +const ranges = require('../../ranges') // would normally be "const ranges = require('@nodevu/ranges')" in your code +const translate = require('../index') // would normally be "const translate = require('@nodevu/translate')" in your code + +async function getTranslatedData () { + // translation for the 'current' Package Support term + const translatedCurrent = await ranges('current') + + // translation for the 'lts_latest' Package Support term + const translatedLtsLatest = await ranges(await translate('lts_latest')) + + // translation for the 'lts' Package Support term + const translatedLts = await ranges(await translate('lts')) + + // translation for the 'supported' Package Support term + const translatedSupported = await ranges(await translate('supported')) + + // translation for the 'all' Package Support term + const translatedAll = await ranges(await translate('all')) + + // return the data in a single object + return { + "current": translatedCurrent, + "lts_latest": translatedLtsLatest, + "lts": translatedLts, + "supported": translatedSupported, + "all": translatedAll + } +} + +getTranslatedData().then(console.log) diff --git a/translate/index.js b/translate/index.js new file mode 100644 index 00000000..9190e6d4 --- /dev/null +++ b/translate/index.js @@ -0,0 +1,18 @@ +// ordered from most recent coverage to oldest coverage +const translations = { + current: ['current'], + lts_latest: ['lts/latest'], + lts: ['lts/active', 'lts/maintenance'], + supported: ['current', 'lts/active', 'lts/maintenance'], + all: ['current', 'lts/active', 'lts/maintenance', 'eol'] +} + +async function translate (legacyName) { + if (!translations[legacyName]) { + throw new Error(`Unknown value: ${legacyName}`) + } else { + return translations[legacyName] + } +} + +module.exports = translate diff --git a/translate/package.json b/translate/package.json new file mode 100644 index 00000000..98d0ea6e --- /dev/null +++ b/translate/package.json @@ -0,0 +1,40 @@ +{ + "name": "@nodevu/translate", + "version": "0.0.1", + "description": "a translation layer between the nodevu naming mechanism and the Node.js supported mechanism. Neceessary for legacy interop.", + "keywords": [ + "nodevu", + "node", + "nodejs", + "versions", + "supported" + ], + "homepage": "https://github.com/cutenode/nodevu#readme", + "bugs": { + "url": "https://github.com/cutenode/nodevu/issues" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/cutenode/nodevu.git" + }, + "license": "MIT", + "author": "Tierney Cyren", + "main": "index.js", + "scripts": { + "coverage": "nyc node--test", + "lint": "standard --env mocha", + "lint:fix": "standard --fix --env mocha", + "lint:packagejson": "npx sort-package-json", + "test": "node--test", + "updates": "npx npm-check-updates" + }, + "devDependencies": { + "@nodevu/ranges": "^0.0.1", + "nyc": "^15.1.0", + "standard": "^17.0.0", + "test": "^3.2.1" + }, + "engines": { + "node": ">=16.0.0" + } +} diff --git a/translate/test/current.test.js b/translate/test/current.test.js new file mode 100644 index 00000000..fc997794 --- /dev/null +++ b/translate/test/current.test.js @@ -0,0 +1,10 @@ +const assert = require('node:assert') +const translate = require('../index') +const { describe, it } = require('test') + +describe('test that translating current works as expected', async () => { + it('should return "current" when passed "current"', async () => { + const result = await translate('current') + assert.deepStrictEqual(result, ['current']) + }) +}) diff --git a/translate/test/ranges.test.js b/translate/test/ranges.test.js new file mode 100644 index 00000000..82a872c0 --- /dev/null +++ b/translate/test/ranges.test.js @@ -0,0 +1,41 @@ +const assert = require('node:assert') +const translate = require('../index') +const ranges = require('@nodevu/ranges') +const { describe, it } = require('test') + +describe('test that each alias works with @nodevu/ranges', async () => { + it('should return the same result when both the translated and untranslated "current" are passed to @nodevu/ranges', async () => { + const untranslated = await ranges('current') + const translated = await ranges(await translate('current')) + + assert.deepStrictEqual(untranslated, translated) + }) + + it('should return the same result when "lts/latest" and the translated "lts_latest" are passed to @nodevu/ranges', async () => { + const untranslated = await ranges('lts/latest') + const translated = await ranges(await translate('lts_latest')) + + assert.deepStrictEqual(untranslated, translated) + }) + + it('should return the same result when "lts/active" and "lts/maintenance" and the translated "lts" are passed to @nodevu/ranges', async () => { + const untranslated = await ranges(['lts/active', 'lts/maintenance']) + const translated = await ranges(await translate('lts')) + + assert.deepStrictEqual(untranslated, translated) + }) + + it('should return the same result when "current", "lts/active", and "lts/maintenance" and the translated "supported" are passed to @nodevu/ranges', async () => { + const untranslated = await ranges(['current', 'lts/active', 'lts/maintenance']) + const translated = await ranges(await translate('supported')) + + assert.deepStrictEqual(untranslated, translated) + }) + + it('should return the same result when "current", "lts/active", "lts/maintenance", and "eol" and the translated "all" are passed to @nodevu/ranges', async () => { + const untranslated = await ranges(['current', 'lts/active', 'lts/maintenance', 'eol']) + const translated = await ranges(await translate('all')) + + assert.deepStrictEqual(untranslated, translated) + }) +})