├── .npmrc ├── .gitattributes ├── .gitignore ├── index.js ├── index.test-d.ts ├── .editorconfig ├── .github └── workflows │ └── main.yml ├── index.d.ts ├── test.js ├── package.json ├── license └── readme.md /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import toSemver from 'to-semver'; 2 | 3 | export default function latestSemver(versions) { 4 | return toSemver(versions, {includePrereleases: false})[0]; 5 | } 6 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import latestSemver from './index.js'; 3 | 4 | expectType( 5 | latestSemver(['v1.8.0-alpha.1', 'v1.3.16', 'v1.7.0', 'v1.6.9']), 6 | ); 7 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: actions/setup-node@v2 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - run: npm install 20 | - run: npm test 21 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Get the latest stable [semver](https://semver.org) version from an array of versions. 3 | 4 | Non-semver versions and prereleases are ignored. 5 | 6 | @example 7 | ``` 8 | import latestSemver from 'latest-semver'; 9 | 10 | latestSemver([ 11 | 'v1.8.0-alpha.1', 12 | 'v1.3.16', 13 | 'v1.7.0', 14 | 'v1.6.9' 15 | ]); 16 | //=> '1.7.0' 17 | 18 | latestSemver([ 19 | 'unicorn', 20 | 'v1.8.0-alpha.1' 21 | ]); 22 | //=> undefined 23 | ``` 24 | */ 25 | export default function latestSemver(versions: readonly string[]): string | undefined; 26 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import latestSemver from './index.js'; 3 | 4 | test('main', t => { 5 | const versions = [ 6 | 'v1.3.16', 7 | 'v1.7.0', 8 | 'v1.6.9', 9 | 'v1.6.8', 10 | 'v1.3.15', 11 | 'v1.6.7', 12 | ]; 13 | 14 | t.is(latestSemver(versions), '1.7.0'); 15 | }); 16 | 17 | test('no versions', t => { 18 | t.is(latestSemver([]), undefined); 19 | t.is(latestSemver(['foo', 'bar']), undefined); 20 | }); 21 | 22 | test('handles prerelease versions', t => { 23 | const versions = [ 24 | '1.2.3-alpha.3', 25 | '1.2.0', 26 | '1.0.0', 27 | ]; 28 | 29 | t.is(latestSemver(versions), '1.2.0'); 30 | }); 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "latest-semver", 3 | "version": "4.0.0", 4 | "description": "Get the latest stable semver version from an array of versions", 5 | "license": "MIT", 6 | "repository": "sindresorhus/latest-semver", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "semver", 27 | "semantic", 28 | "version", 29 | "versioning", 30 | "latest", 31 | "stable", 32 | "max", 33 | "maximum", 34 | "highest", 35 | "newest", 36 | "git", 37 | "tag", 38 | "tags" 39 | ], 40 | "dependencies": { 41 | "to-semver": "^4.0.0" 42 | }, 43 | "devDependencies": { 44 | "ava": "^3.15.0", 45 | "tsd": "^0.18.0", 46 | "xo": "^0.45.0" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # latest-semver 2 | 3 | > Get the latest stable [semver](https://semver.org) version from an array of versions 4 | 5 | This can be useful when you have an unsorted list of versions, like Git tags, and want to get the semantically latest version. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install latest-semver 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import latestSemver from 'latest-semver'; 17 | 18 | latestSemver([ 19 | 'v1.8.0-alpha.1', 20 | 'v1.3.16', 21 | 'v1.7.0', 22 | 'v1.6.9' 23 | ]); 24 | //=> '1.7.0' 25 | 26 | latestSemver([ 27 | 'unicorn', 28 | 'v1.8.0-alpha.1' 29 | ]); 30 | //=> undefined 31 | ``` 32 | 33 | ## API 34 | 35 | Non-semver versions and prereleases are ignored. 36 | 37 | ### latestSemver(versions) 38 | 39 | #### versions 40 | 41 | Type: `string[]` 42 | 43 | ## Related 44 | 45 | - [to-semver](https://github.com/sindresorhus/to-semver) - Get an array of valid, sorted, and cleaned semver versions from an array of strings 46 | - [semver-regex](https://github.com/sindresorhus/semver-regex) - Regular expression for matching semver versions 47 | - [semver-diff](https://github.com/sindresorhus/semver-diff) - Get the diff type of two semver versions: `0.0.1` `0.0.2` → `patch` 48 | - [semver-truncate](https://github.com/sindresorhus/semver-truncate) - Truncate a semver version: `1.2.3` → `1.2.0` 49 | --------------------------------------------------------------------------------