Socket
Book a DemoInstallSign in
Socket

@momsfriendlydevco/readable

Package Overview
Dependencies
Maintainers
5
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@momsfriendlydevco/readable

Utility formatters to take raw data and present it in a readable format

latest
Source
npmnpm
Version
1.2.1
Version published
Maintainers
5
Created
Source

@momsfriendlydevco/readable

Utility formatters to take raw data and present it in a readable format.

Features:

  • No dependencies
  • Tiny footprint
  • Customizable time / size units
  • Code is as small as possible and easily copy/pasteable

API

readable.defaults

Obejct containing all the default settings. These are deep merged with any setting object given to a formatter.

readable.fileSize(bytes, [options])

Return a human readable file size.

readable.fileSize(1024) //= "1kb"
readable.fileSize(1536) //= "1.5kb"
readable.fileSize(1048576) //= "1mb"
readable.fileSize(1073741824) //= "1tb"
readable.fileSize(1288490188) //= "1.2tb"

This function can take the following options:

OptionTypeDefaultDescription
decimalsnumber1Decimal places to format the number into
decimalsAbsolutebooleantrueIf true the decimals will be clipped entirely if the number is exactly correct (e.g. if the input is 1024 the output is 1kb not 1.0kb)
unitsObject(See code)A list of units to output, each is named in the plural with a boolean indicating if it should be reported e.g. {units: {years: true}} to enable years as a usable unit
formattersObject(See code)A list of formatters for each named unit, each should be a function taking a single value. e.g. {formatters: {days: v => \${v} days`}}`
formatters.fallbackFunction or String""The fallback formatter to use, if it is a string its used as is, if it is a function it is called with the value
valuesObject(See code)A list of unit values to work with if the unit is enabled, each is the plural key of the measure with the number of milliseconds within the unit as the key

readable.relativeTime(timestamp, [options])

Return a human readable relative time distance.

readable.relativeTime(new Date(Date.now() - 50)) //= "Just now"
readable.relativeTime(new Date(Date.now() - 50), {units: {milliseconds: true}}) //= "50ms"
readable.relativeTime(Date.now() - 5000) //= "5s"
readable.relativeTime(new Date(Date.now() - 60000)) //= "1m"
readable.relativeTime(new Date(Date.now() - 65000)) //= "1m5s"
readable.relativeTime(moment().subtract(1, 'w').subtract(2, 'd')) //= "9D"

This function can take the following options:

OptionTypeDefaultDescription
unitsObject(See code)A list of units to output, each is named in the plural with a boolean indicating if it should be reported e.g. {units: {years: true}} to enable years as a usable unit
formattersObject(See code)A list of formatters for each named unit, each should be a function taking a single value. e.g. {formatters: {days: v => \${v} days`}}`
formatters.fallbackFunction or String"Just now"The fallback formatter to use, if it is a string its used as is, if it is a function it is called with the value
formatters.inputFunction(See code)Input mangler, by defaults this converts dates and Moment objects to a millisecond offset from now
formatters.combinerFunction(See code)Function used to combine all compiled units, by default this joins everything into a string with no spacing and truncates any training spaces
valuesObject(See code)A list of unit values to work with if the unit is enabled, each is the plural key of the measure with the number of milliseconds within the unit as the key

readable.sizeOf(data, [options])

Calculate the (approximate in some cases) size of a variable as quickly as possible.

readable.sizeOf(123) //= 4 (JS uses 64bit integers)
readable.sizeOf('a') //= 3 (1 byte + enclosing speachmarks)
readable.sizeOf({}) //= 2 (enclosing braces only)
readable.sizeOf({foo: 'abc'}) //= 14

The following assumptions are made:

  • Whitespace for stringified data is ignored
  • Strings are 1 char to 1 byte (unless options.stringDeepScan is specified)
  • Numbers are 64 bit / 2 bytes
  • Circular references are resolved to a given byte size (see options.circular)

This function can take the following options:

OptionTypeDefaultDescription
circularboolean or numberfalseCalculate circular references as this byte value, if boolean false references are not checked
stringDeepScanbooleanfalseCalculate additional UTf-8 storage space
stringOverheadnumber2Additional bytes for string storage, set to 2 to store enclosing speachmarks
fallbackfunction(See code)Function to use to calculate all non-handled types (defaults to JSON.stringify length)

Keywords

readable

FAQs

Package last updated on 14 Feb 2020

Did you know?

Socket

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.

Install

Related posts