structuralists / humanize-key

format string keys for display

Home Page:https://www.npmjs.com/package/humanize-key

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Format string keys for display

CI

Publish

NPM version

Installation

  • npm install humanize-key

Usage

import humanizeKey from 'humanize-key'
// or
const { humanizeKey } = require('humanize-key')

const displayString = humanizeKey(key)
const display = (obj) => {
   forEach(obj, (value, key) => console.log(`${humanizeKey(key)}: ${value}`))
}
display({
   id: 12,
   userName: 'bobby',
   'first-name': 'Bob',
   last_name: 'Roberts',
   account_id: 321,
})
ID: 12
User Name: bobby
First Name: Bob
Last Name: Roberts
Account ID: 321

currently there are two exemptions to the default capitalization

  • id -> ID rather than Id
  • ids -> IDsrather than Ids

other custom values can be set by use of makeHumanizeKey

Custom values

domains-specific acronyms

import { makeHumanizeKey } from 'humanize-key'
const humanizeKey = makeHumanizeKey({ acronyms: ['IRS', 'SSN'] })
const obj = {
   id: 12,
   ssn_last_four: '1234',
   'irs-account-number': '5678',
}
ID: 12
SSN Last Four: 1234
IRS Account Number: 5678

custom capitalization for terms of art

import { makeHumanizeKey } from 'humanize-key'
const humanizeKey = makeHumanizeKey({
   acronyms: ['URL']
   uniques: {
      oauth: "OAuth",
      uuids: "UUIDs",
   }
})
display({
   oauth_url: 'http://localhost:3000/oauth/callback',
   recent_uuids: [],
})
OAuth URL: http://localhost:3000/oauth/callback
Recent UUIDs: []

About

format string keys for display

https://www.npmjs.com/package/humanize-key

License:MIT License


Languages

Language:TypeScript 100.0%