New:Socket for Asana Is Now Available.Learn more
Get Started

@isoftdata/utility-string

Package Overview
Dependencies
Maintainers
11
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@isoftdata/utility-string

![Node.js CI](https://github.com/ISoft-Data-Systems/utility-string/workflows/Node.js%20CI/badge.svg)

npmnpm
Version
2.1.0
Version published
Weekly downloads
55
-57.03%
Maintainers
11
Weekly downloads
 
Created
Source

Node.js CI

Breaking change in version 2: ESM style exports

Methods

  • pluralIfNeeded
  • stringToBoolean
  • booleanToYesNo

pluralIfNeeded

API

pluralIfNeeded(number[, pluralSuffix, singleSuffix])

number

Type: number

pluralSuffix

Type: string

Default: 's'

singleSuffix

Type: string

Default: ''(empty string)

Returns

string

Example


//Simple Example
const inventoryList = [ { inventoryId: 1 }, { inventoryId: 2 } ]

`You have ${inventoryList.length} item${pluralIfNeeded(inventoryList.length)} in inventory` // 'You have 2 items in inventory'

//Example with custom plural suffix and single suffix
const daytonOxenCount = 0
const billOxenCount = 1
const tedOxenCount = 2

`Dayton has ${daytonOxenCount} ox${pluralIfNeeded(daytonOxenCount, 'en')}` // 'Dayton has 0 oxen'
`Bill has ${billOxenCount} ox${pluralIfNeeded(billOxenCount, 'en')}` // 'Bill has 1 ox'
`Ted has ${tedOxenCount} ox${pluralIfNeeded(tedOxenCount, 'en')}` // 'Ted has 2 oxen'

//Another example with custom plural suffix and single suffix
const daytonCactusCount = 0
const billCactusCount = 1
const tedCactusCount = 2

`Dayton has ${daytonCactusCount} cact${pluralIfNeeded(daytonCactusCount, 'i', 'us')}` // 'Dayton has 0 cactus'
`Bill has ${billCactusCount} cact${pluralIfNeeded(billCactusCount, 'i', 'us')}` // 'Bill has 1 cacti'
`Ted has ${tedCactusCount} cact${pluralIfNeeded(tedCactusCount, 'i', 'us')}` // 'Ted has 2 cactus'

stringToBoolean

API

stringToBoolean(string)

string

Type: string

Returns

boolean

Example

const inventoryItem = { inventoryId: 1, usesCost: 'True' }
stringToBoolean(inventoryItem.usesCost) // true

booleanToYesNo

API

booleanToYesNo(boolean)

boolean

Type: boolean

Returns

string

Example

booleanToYesNo(true) // 'Yes'
booleanToYesNo(false) // 'No'

translate

Designed to be a drop-in replacement for i18next's .t function that you can use as a default in your translation-aware componments/code as a fallback for consumers that don't have translation/i18next support. It supports the same syntax as i18next's .t function, but does not do translation, just uses the default string you pass. It does support the most common advanced use cases, including interpolation via i18next's iterpolation syntax like {{example}} and {{- example}}.

API

translate('foo', 'blah blah {{foo}} bar', { foo: 'candy' }) // 'blah blah candy bar'

args

(key: string, defaultValue: string, options?: Record<string, string>)

Returns

string

Example

translate('foo', 'what is this?') // 'what is this?'
translate('foo', 'what the {{- curse}} is this?', { curse: 'heck' }) // 'what the heck is this?'

FAQs

Package last updated on 03 Feb 2025

Related posts