├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── index.test-d.ts ├── test.js ├── index.d.ts ├── index.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 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/semver-truncate 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import semverTruncate from './index.js'; 3 | 4 | expectType(semverTruncate('1.2.3-foo', 'patch')); 5 | expectType(semverTruncate('1.2.3', 'minor')); 6 | expectType(semverTruncate('1.2.3', 'major')); 7 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import semverTruncate from './index.js'; 3 | 4 | test('main', t => { 5 | t.is(semverTruncate('1.2.3-foo', 'patch'), '1.2.3'); 6 | t.is(semverTruncate('1.2.3+foo', 'patch'), '1.2.3'); 7 | t.is(semverTruncate('1.2.3', 'minor'), '1.2.0'); 8 | t.is(semverTruncate('1.2.3', 'major'), '1.0.0'); 9 | t.is(semverTruncate('7.0.8-0ubuntu0.16.04.2', 'patch'), '7.0.8', 'PHP on Ubuntu'); 10 | }); 11 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Truncate a semver version: `1.2.3` → `1.2.0`. 3 | 4 | @param version - Semver version. 5 | @param type - Version type to truncate to. 6 | 7 | @example 8 | ``` 9 | import semverTruncate from 'semver-truncate'; 10 | 11 | semverTruncate('1.2.3-foo', 'patch'); 12 | //=> '1.2.3' 13 | 14 | semverTruncate('1.2.3', 'minor'); 15 | //=> '1.2.0' 16 | 17 | semverTruncate('1.2.3', 'major'); 18 | //=> '1.0.0' 19 | ``` 20 | */ 21 | export default function semverTruncate(version: string, type: 'patch' | 'minor' | 'major'): string; 22 | -------------------------------------------------------------------------------- /.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 | - 14 15 | - 12 16 | steps: 17 | - uses: actions/checkout@v2 18 | - uses: actions/setup-node@v2 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import semver from 'semver'; 2 | 3 | export default function semverTruncate(version, type) { 4 | if (!['major', 'minor', 'patch'].includes(type)) { 5 | throw new TypeError(`Invalid version type: ${version}`); 6 | } 7 | 8 | version = semver.parse(version, {loose: true}); 9 | 10 | if (!version) { 11 | throw new Error(`Version ${version} is not valid semver`); 12 | } 13 | 14 | version.build = ''; 15 | version.prerelease = ''; 16 | 17 | if (type === 'minor') { 18 | version.patch = 0; 19 | } 20 | 21 | if (type === 'major') { 22 | version.patch = 0; 23 | version.minor = 0; 24 | } 25 | 26 | return version.format(); 27 | } 28 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "semver-truncate", 3 | "version": "3.0.0", 4 | "description": "Truncate a semver version: `1.2.3` → `1.2.0`", 5 | "license": "MIT", 6 | "repository": "sindresorhus/semver-truncate", 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" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "semver", 27 | "version", 28 | "semantic", 29 | "truncate", 30 | "shorten", 31 | "simplify" 32 | ], 33 | "dependencies": { 34 | "semver": "^7.3.5" 35 | }, 36 | "devDependencies": { 37 | "ava": "^3.15.0", 38 | "tsd": "^0.14.0", 39 | "xo": "^0.39.1" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /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 | # semver-truncate 2 | 3 | > Truncate a semver version: `1.2.3` → `1.2.0` 4 | 5 | ## Install 6 | 7 | ``` 8 | $ npm install semver-truncate 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import semverTruncate from 'semver-truncate'; 15 | 16 | semverTruncate('1.2.3-foo', 'patch'); 17 | //=> '1.2.3' 18 | 19 | semverTruncate('1.2.3', 'minor'); 20 | //=> '1.2.0' 21 | 22 | semverTruncate('1.2.3', 'major'); 23 | //=> '1.0.0' 24 | ``` 25 | 26 | ## API 27 | 28 | ### truncateSemver(version, type) 29 | 30 | #### version 31 | 32 | Type: `string` 33 | 34 | Semver version. 35 | 36 | #### type 37 | 38 | Type: `'patch' | 'minor' | 'major'` 39 | 40 | Version type to truncate to. 41 | 42 | ## Related 43 | 44 | - [latest-semver](https://github.com/sindresorhus/latest-semver) - Get the latest stable semver version from an array of versions 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 | --------------------------------------------------------------------------------