首页 > 其他分享 >版本对比的库

版本对比的库

时间:2023-11-24 19:55:22浏览次数:45  
标签:10.0 compare 1.0 versions version 版本 satisfies 对比

compare-versions

TypeScript icon, indicating that this package has built-in type declarations 6.1.0 • Public • Published 4 months ago

compare-versions

Build Status Coverage Status npm bundle size (minified + gzip)

Compare semver version strings to find greater, equal or lesser. Runs in the browser as well as Node.js/React Native etc. Has no dependencies and is tiny.

Supports the full semver specification including versions with different number of digits like 1.0.01.01 and pre-releases like 1.0.0-alpha. Additionally supports the following variations:

  • Wildcards for minor and patch version like 1.0.x or 1.0.*.
  • Chromium version numbers with 4 parts, e.g. version 25.0.1364.126.
  • Any leading v is ignored, e.g. v1.0 is interpreted as 1.0.
  • Leading zero is ignored, e.g. 1.01.1 is interpreted as 1.1.1.
  • npm version ranges, e.g. 1.2.7 || >=1.2.9 <2.0.0

Install

$ npm install compare-versions

Note: Starting from v5 the main export is now named like so: import { compareVersions } from 'compare-versions'.

Note: Starting from v4 this library includes a ESM version which will automatically be selected by your bundler (webpack, parcel etc). The CJS/UMD version is lib/umd/index.js and the new ESM version is lib/esm/index.js.

Usage

Will return 1 if first version is greater, 0 if versions are equal, and -1 if the second version is greater:

import { compareVersions } from 'compare-versions';

compareVersions('11.1.1', '10.0.0'); //  1
compareVersions('10.0.0', '10.0.0'); //  0
compareVersions('10.0.0', '11.1.1'); // -1

Can also be used for sorting:

const versions = [
  '1.5.19',
  '1.2.3',
  '1.5.5'
]
const sorted = versions.sort(compareVersions);
/*
[
  '1.2.3',
  '1.5.5',
  '1.5.19'
]
*/

"Human Readable" Compare

The alternative compare function accepts an operator which will be more familiar to humans:

import { compare } from 'compare-versions';

compare('10.1.8', '10.0.4', '>');  // true
compare('10.0.1', '10.0.1', '=');  // true
compare('10.1.1', '10.2.2', '<');  // true
compare('10.1.1', '10.2.2', '<='); // true
compare('10.1.1', '10.2.2', '>='); // false

Version ranges

The satisfies function accepts a range to compare, compatible with npm package versioning:

import { satisfies } from 'compare-versions';

satisfies('10.0.1', '~10.0.0');  // true
satisfies('10.1.0', '~10.0.0');  // false
satisfies('10.1.2', '^10.0.0');  // true
satisfies('11.0.0', '^10.0.0');  // false
satisfies('10.1.8', '>10.0.4');  // true
satisfies('10.0.1', '=10.0.1');  // true
satisfies('10.1.1', '<10.2.2');  // true
satisfies('10.1.1', '<=10.2.2'); // true
satisfies('10.1.1', '>=10.2.2'); // false
satisfies('1.4.6', '1.2.7 || >=1.2.9 <2.0.0'); // true
satisfies('1.2.8', '1.2.7 || >=1.2.9 <2.0.0'); // false
satisfies('1.5.1', '1.2.3 - 2.3.4'); // true
satisfies('2.3.5', '1.2.3 - 2.3.4'); // false

Validate version numbers

Applies the same rules used comparing version numbers and returns a boolean:

import { validate } from 'compare-versions';

validate('1.0.0-rc.1'); // true
validate('1.0-rc.1');   // false
validate('foo');        // false

Validate version numbers (strict)

Validate version numbers strictly according to semver.org; 3 integers, no wildcards, no leading zero or "v" etc:

import { validateStrict } from 'compare-versions';

validate('1.0.0');      // true
validate('1.0.0-rc.1'); // true
validate('1.0');        // false
validate('1.x');        // false
validate('v1.02');      // false

Browser

If included directly in the browser, the functions above are available on the global window under the compareVersions object:

<script src=https://unpkg.com/compare-versions/lib/umd/index.js></script>
<script>
  const { compareVersions, compare, satisfies, validate } = window.compareVersions
  console.log(compareVersions('11.0.0', '10.0.0'))
  console.log(compare('11.0.0', '10.0.0', '>'))
  console.log(satisfies('1.2.0', '^1.0.0'))
  console.log(validate('11.0.0'))
  console.log(validateStrict('11.0.0'))
</script>

Readme

Keywords

标签:10.0,compare,1.0,versions,version,版本,satisfies,对比
From: https://www.cnblogs.com/sexintercourse/p/17854627.html

相关文章

  • 手写线程池——C和C++版本
    内容参考:爱编程的大丙(subingwen.cn)C语言版大致思路采用生产者——消费者模型:生产者:用户向任务队列添加任务,是生产者。消费者:线程池里面的线程从任务队列中取出任务是,是消费者。任务队列:单个任务结构:使用结构体封装,其中包含一个函数指针,用于指向要处理的具体任务......
  • pageoffice6 版本实现word 文件添加水印
    在很多场景下,Word文档正式发文之前,或者说形成最终文档之前,常常需要往Word文件中添加水印,并且会根据文件类型或内容的不同,需要添加的水印也不一样。添加水印是Word软件里的一个简单功能,直接点击Word工具栏中的水印按钮就可以手动添加,但是在Web项目开发过程中,时常会遇到通过调用程......
  • Linux如何查看系统版本
    https://baijiahao.baidu.com/s?id=1780057482158241756&wfr=spider&for=pc在Linux系统中,查看系统版本是非常重要的,因为它可以帮助我们了解系统的基本情况,包括内核版本、发行版类型等等。以下是几种常见的查看Linux系统版本的方法:一、使用命令行查看1、使用uname命令uname-a......
  • 5.mysql8.0以上版本,ProxySQL 监控/连接账户,要以 mysql_native_password 形式创建,否则
    CREATEUSER'monitor'@'%'IDENTIFIEDBY'123456';grantallprivilegeson*.*to'monitor'@'%'withgrantoption;flushprivileges; ALTERUSER'root'@'%'IDENTIFIEDWITHmysql_native_pa......
  • 获取apk 版本号 版本名
    文章目录1、适用范围2、AndroidManifest.xml添加需要的版本号版本名2、代码里获取版本号版本名1、适用范围有的时候会需要对apk进行版本升级,或者区分apk更新版本等,会用到apk版本名,版本号等问题。2、AndroidManifest.xml添加需要的版本号版本名如图:我们添加的版......
  • nacos 2.2.3版本开启登录认证
    server.tomcat.basedir=/root/nacos #此路径必须存在nacos.core.auth.system.type=nacosnacos.core.auth.enabled=truenacos.core.auth.server.identity.key=serverIdentitynacos.core.auth.server.identity.value=securitynacos.core.auth.plugin.nacos.token.secret.key=S......
  • 【问题记录】【IDEA工具】升级了个版本- -启动报错 com.intellij.ide.util.Properties
    1 启动报错Causedby:java.lang.ClassNotFoundException:com.intellij.ide.util.PropertiesComponentImplPluginClassLoader(plugin=PluginDescriptor(name=BetterIntelliJ,id=org.example.BetterIntelliJ,descriptorPath=plugin.xml,path=~/Library/ApplicationSuppor......
  • 蓝牙版本
    我的电脑运行哪个蓝牙版本?蓝牙配件有时需要满足最低的蓝牙规格(版本)要求,才能充分发挥功能。查看你电脑上的蓝牙版本在任务栏上的搜索框中,键入“设备管理器”,然后从结果列表中选择它。选择“蓝牙”旁边的箭头以将其展开。选择蓝牙无线收发器列表(你的蓝牙无线收发器可能只......
  • 获取微信版本、手机操作系统、手机类型
    效果图letsystem={}letuserAgent=navigator.userAgent//获取微信版本letsystemInfo=userAgent.match(/MicroMessenger.*?(?=)/)if(systemInfo&&systemInfo.length>0){system.wechat=systemInfo[0]}//苹果手机......
  • windows版本--人大金仓数据库连接报错----启动----及替换过期的授权文件
       启动服务命令:1、找到安装目录下server的bin进行cmd C:\ProgramFiles\Kingbase\ES\V8\KESRealPro\V008R006C007B0012\Server\bin2、执行命令:sys_ctl.exe-D"data的存放目录"startsys_ctl.exe-D"C:\ProgramFiles\Kingbase\ES\V8\data"start 3、去官网根......