Skip to content

whisklabs/amounts

Repository files navigation

Whisk formatter library for amounts

Description

Functions for beautify amount of food.

Install

npm i @whisklabs/amounts

Usage

import { formatQuantity } from '@whisklabs/amounts';

formatQuantity(100);

Methods

Format values

const formatQuantity: (quantity: number, unit?: string | undefined) => string;

formatQuantity(12.12, 'tsp') === '12 ⅛';

Capitalize some unit (tbs, tbsp)

const formatUnit: (unit: string) => string;

formatUnit('grms') = 'grms';
formatUnit('tbsp') = 'Tbsp';

Format with unit

const formatQtyUnit: (item: { quantity?: number; unit?: string }) => string;

formatQtyUnit({ quantity: 1123.5, unit: 'tsp' }) === '1123 ½ tsp';

Get UTF-8 fraction

const formatFraction: (fraction: string) => string;

formatFraction('1/2') === '½';

Round numbers

const dropZeros: (value: number, decimalPoints?: number) => number;

dropZeros(1.014634, 3) === '1.015';

Get round of fraction

const getFractions: (value: number) => [number, number, number] | undefined;

getFractions(10.33333333) === [10, 1, 3];

Check unit fraction

const isFractionalUnit: (unit: string) => boolean;

isFractionalUnit('kg') === false;
isFractionalUnit('tablespoon') === true;

Check unit capitalize

const isCapitalizableUnit: (unit: string) => boolean;

isCapitalizableUnit('kg') === false;
isCapitalizableUnit('tbs') === true;

Check unit white space

const isWithoutSpaceUnit: (unit: string) => boolean;

isWithoutSpaceUnit('grms') === false;
isWithoutSpaceUnit('ml') === true;

Constants

Fractions

For convert '1/2' -> '½'

const fractions: {
  [key: string]: string;
};

Units

kg, ml, ...

const units: Partial<{
  [unit: string]: {
    fractional?: true;
    withoutSpace?: true;
    capitalizable?: true;
  };
}>;