
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
@soundxyz/ms
Advanced tools
Use this package to easily convert various time formats to milliseconds.
ms('2 days') // 172800000
ms('1d') // 86400000
ms('10h') // 36000000
ms('2.5 hrs') // 9000000
ms('2h') // 7200000
ms('1m') // 60000
ms('5s') // 5000
ms('1y') // 31557600000
ms('100') // 100
ms('-3 days') // -259200000
ms('-1h') // -3600000
ms('-200') // -200
ms(60000) // "1m"
ms(2 * 60000) // "2m"
ms(-3 * 60000) // "-3m"
ms(ms('10 hours')) // "10h"
ms(60000, { long: true }) // "1 minute"
ms(2 * 60000, { long: true }) // "2 minutes"
ms(-3 * 60000, { long: true }) // "-3 minutes"
ms(ms('10 hours'), { long: true }) // "10 hours"
ms
, a string with a unit is returned100
for '100'
)As of v3.0
, this package includes TypeScript definitions.
For added safety, we're using Template Literal Types (added in TypeScript 4.1). 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.
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 to coerce a string
.
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.
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');
ms
is compatible with the Edge Runtime. It can be used inside environments like Vercel Edge Functions as follows:
// Next.js (pages/api/edge.js) (npm i next@canary)
// Other frameworks (api/edge.js) (npm i -g vercel@canary)
import ms from 'ms';
const start = Date.now();
export default (req) => {
return new Response(`Alive since ${ms(Date.now() - start)}`);
};
export const config = {
runtime: 'experimental-edge',
};
ms
as a macro at build-time.npm link
npm link ms
. Instead of the default one from npm, Node.js will now use your clone of ms!As always, you can run the tests using: npm test
FAQs
Tiny millisecond conversion utility
The npm package @soundxyz/ms receives a total of 311 weekly downloads. As such, @soundxyz/ms popularity was classified as not popular.
We found that @soundxyz/ms demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 18 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.