npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

orange-smart-time

v0.1.0

Published

Orange.SmartTime - library for converting timestamps to fancy string like "2 days ago"

Downloads

1

Readme

Orange Smart Time

Library for converting timestamps into texts like "2 hours ago", "Yesterday", etc.

OrangeSmartTime class

Methods

Constructor

| Argument | Type | Default | Description | |---|---|---|---| | texts | object, null | null | Object with translation (by default English is being used) | | timespansRoundLogic | boolean | false | Flag if class should use "round" login instead of "floor" logic for converting numbers for timespans like "2 hours ago" into integers |

convertTimestamp

Converts timestamp (seconds) into text in relation to the current moment.

| Argument | Type | Default | Description | |---|---|---|---| | timestamp | number | | Timestamp like 1629935777 |

convertTimestampMs

Converts timestamp (milliseconds) into text in relation to the current moment.

| Argument | Type | Default | Description | |---|---|---|---| | timestampMs | number | | Timestamp like 1629935777000 |

convertTimespan

Converts timespan (in seconds) into text

| Argument | Type | Default | Description | |---|---|---|---| | seconds | number | | Number of seconds like 300 (it will be converted to 5 minutes) |

convertTimespanMs

Converts timespan (in milliseconds) into text

| Argument | Type | Default | Description | |---|---|---|---| | milliseconds | number | | Number of milliseconds like 300000 (it will be converted to 5 minutes) |

Properties

You can set properties to modify behavior of the library.

| Property | Type | Default | Description | |---|---|---|---| | now | number | current timestamp | Current timestamp in seconds (overrides current timestamp with static value). Affects nowMs property too. | | nowMs | number | current timestamp | Current timestamp in milliseconds (overrides current timestamp with static value). Affects now property too. | | weeks | number | 5 | Number of weeks when phrase like "N weeks ago" will have priority over "N months ago". For example something happened 35 days ago. If this property is >= 5 it will be converted into "5 weeks ago", in other case it will be "1 month ago" | | lastTexts | boolean | false | Allow date-based texts like "last year", "yesterday", etc. for the past | | nextTexts | boolean | false | Allow date-based texts like "next year", "tomorrow", etc. for the future | | lastNextTextMinDelta | number | 7200 | Minimal delta (in seconds) for showing "last ..."-type of texts | | lastNextToIntegerFunction | function | v => Math.round(v) or v => Math.floor(v) | Function which is being used to calculate integer number of time timespans (hours, days, months, etc.) based on delta | | stringNowSecondsLimit | number | 5 | Maximal number of seconds identified as "now" |

Example

const OrangeSmartTime = require('orange-smart-time')
const ost = new OrangeSmartTime()
const timestamp = ost.now - 10 // Obviously you don't need to calculate it like that, because you already have timestamp you want to convert
const text = ost.convertTimestamp(timestamp)
console.log(text) // Prints "10 seconds ago" 

Translations

You can provide translations object in the first argument of the class constructor.

Available translations

  • Russian: languages/ru.json

Create your own

If you want to create your own translation, use file languages/en-basic.json as example. It contains all required keys for generating texts. Besides, your language may require more customization - look at languages/en-extended.json.

For example:

  • To define spelling for specific number (1) or hours - define key hours:1: "hours:1": "hour"
  • To define spelling for numbers ending with some number (2) use hours%2 (works for 0-99 range). For example in Russian translation we have this: "hours%2": "часа"

To do

  • Add support of "last week" and "next week" texts