Socket
Socket
Sign inDemoInstall

ms

Package Overview
Dependencies
0
Maintainers
79
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.1.3 to 3.0.0-beta.0

lib/index.cjs

72

package.json
{
"name": "ms",
"version": "2.1.3",
"version": "3.0.0-beta.0",
"description": "Tiny millisecond conversion utility",
"repository": "vercel/ms",
"main": "./index",
"main": "./lib/index.cjs",
"type": "module",
"exports": {
"import": "./lib/index.mjs",
"require": "./lib/index.cjs"
},
"module": "./lib/index.mjs",
"types": "./lib/index.d.ts",
"sideEffects": false,
"license": "MIT",
"engines": {
"node": ">=12.13"
},
"files": [
"index.js"
"lib"
],
"scripts": {
"test": "jest",
"build": "scripts/build.js",
"prepublishOnly": "npm run build",
"eslint-check": "eslint --max-warnings=0 .",
"prettier-check": "prettier --check .",
"type-check": "tsc --noEmit",
"precommit": "lint-staged",
"lint": "eslint lib/* bin/*",
"test": "mocha tests.js"
"prepare": "husky install"
},
"eslintConfig": {
"extends": "eslint:recommended",
"env": {
"node": true,
"es6": true
}
"jest": {
"preset": "ts-jest",
"testEnvironment": "node"
},
"prettier": {
"endOfLine": "lf",
"tabWidth": 2,
"printWidth": 80,
"singleQuote": true,
"trailingComma": "all"
},
"lint-staged": {
"*.js": [
"npm run lint",
"prettier --single-quote --write",
"git add"
"*": [
"prettier --ignore-unknown --write"
],
"*.{js,jsx,ts,tsx}": [
"eslint --max-warnings=0 --fix"
]
},
"license": "MIT",
"devDependencies": {
"eslint": "4.18.2",
"expect.js": "0.3.1",
"husky": "0.14.3",
"lint-staged": "5.0.0",
"mocha": "4.0.1",
"prettier": "2.0.5"
"@types/jest": "27.0.1",
"@typescript-eslint/eslint-plugin": "4.29.2",
"@typescript-eslint/parser": "4.29.2",
"eslint": "7.32.0",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-jest": "24.4.0",
"eslint-plugin-tsdoc": "0.2.14",
"husky": "7.0.1",
"jest": "27.0.6",
"lint-staged": "11.1.2",
"prettier": "2.3.2",
"ts-jest": "27.0.5",
"typescript": "4.3.5"
}
}

@@ -9,2 +9,3 @@ # ms

<!-- prettier-ignore -->
```js

@@ -27,2 +28,3 @@ ms('2 days') // 172800000

<!-- prettier-ignore -->
```js

@@ -37,2 +39,3 @@ ms(60000) // "1m"

<!-- prettier-ignore -->
```js

@@ -52,2 +55,63 @@ ms(60000, { long: true }) // "1 minute"

## TypeScript support
As of v3.0, this package includes TypeScript definitions.
For added safety, we're using [Template Literal Types](https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html) (added in [TypeScript 4.1](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-1.html)). This ensures that you don't accidentally pass `ms` values that it can't process.
This won't require you to do anything special in most situations, but you can also import the `StringValue` type from `ms` if you need to use it.
```ts
import ms, { StringValue } from 'ms';
// Using the exported type.
function example(value: StringValue) {
ms(value);
}
// This function will only accept a string compatible with `ms`.
example('1 h');
```
In this example, we use a [Type Assertion](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions) to coerce a `string`.
```ts
import ms, { StringValue } from 'ms';
// Type assertion with the exported type.
function example(value: string) {
try {
// A string could be "wider" than the values accepted by `ms`, so we assert
// that our `value` is a `StringValue`.
//
// It's important to note that this can be dangerous (see below).
ms(value as StringValue);
} catch (error: Error) {
// Handle any errors from invalid vaues.
console.error(error);
}
}
// This function will accept any string, which may result in a bug.
example('any value');
```
You may also create a custom Template Literal Type.
```ts
import ms from 'ms';
type OnlyDaysAndWeeks = `${number} ${'days' | 'weeks'}`;
// Using a custom Template Literal Type.
function example(value: OnlyDaysAndWeeks) {
// The type of `value` is narrower than the values `ms` accepts, which is
// safe to use without coercion.
ms(value);
}
// This function will accept "# days" or "# weeks" only.
example('5.2 days');
```
## Related Packages

@@ -54,0 +118,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc