1.0.1 • Published 2 years ago

@zerodep/case.sentence v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@zerodep/case.sentence

minified size minified+gzipped size tree shaking language types

Code Quality LGTM Grade Maintainability

coverage last commit vulnerabilities

A utility to convert a string to sentence case and strips out non-alphanumeric characters

This utility is designed for converting strings to variable names suitable for database keys and similar use-cases. It will:

  • strip any leading characters that aren't letters (A to Z upper- or lowercase)
  • replace non-alphanumeric characters with underscores
  • format to lowercase with a single space separator between words

tl;dr

A short explanation / quick reference:

import { caseSentence } from '@zerodep/case.sentence';

// using the default formatter (intelligently guesses your locale & currency)
caseSentence('I was a sentence'); // "i was a sentence"
caseSentence('from-kebab-case'); // "from kebab case"
caseSentence('1234 special !@#$ chars'); // "special chars"

Table of Contents

Install

This utility is available from multiple @zerodep packages, enabling developers to select the most appropriately sized package (for both kb and capability) for different use cases. We believe one size does not fit all or most. See @zerodep/app and @zerodep/case.

// all @zerodep features, capabilities and utilities
npm install @zerodep/app

// all @zerodep "case" converters
npm install @zerodep/case

// only the case.sentence package
npm install @zerodep/case.sentence

Of course, you may use yarn, pnpm, or the package manager of your choice. Only npm examples are shown for brevity.

This package may also be found on both jsDelivr and unpkg in UMD, ESM and CJS formats.

How to Use

This package exports the following:

  • Functions
    • caseSentence - formats a string to sentence case
  • Error types
    • ZeroDepError - thrown if the value to format is not a string, is an instance of the base Error object

Signature

Typescript declarations:

declare const caseSentence: (value: string) => string;

Examples

All examples assume ESM or CJS packages. If using a UMD package remember to prefix with the zd namespace, e.g. zd.caseSentence(...).

import { caseSentence } from '@zerodep/case.sentence';

// strings
caseSentence('UPPER CASE'); // "upper case"
caseSentence('snake case'); // "snake case"
caseSentence('kebab-case'); // "kebab case"
caseSentence('camelCase'); // "camel case"
caseSentence('PascalCase'); // "pascal case"
caseSentence('__with_____many    spaces and underscores__'); // "with many spaces and underscores"
caseSentence("I'm a sp3c!al $741ng"); // "i m a sp3 c al 741 ng"
caseSentence('1234 leading numbers'); // "leading numbers"

// integers
caseSentence(42); // throws ZeroDepError
caseSentence(3e8); // throws ZeroDepError

// floats
caseSentence(-273.15); // throws ZeroDepError
caseSentence(Math.PI); // throws ZeroDepError

// number-ish
caseSentence(Number.POSITIVE_INFINITY); // throws ZeroDepError
caseSentence(NaN); // throws ZeroDepError

// bigints
caseSentence(8675309n); // throws ZeroDepError

// object literals
caseSentence({}); // throws ZeroDepError
caseSentence({ a: 'one', b: 'two' }); // throws ZeroDepError

// arrays
caseSentence([]); // throws ZeroDepError
caseSentence([1, 2, 3]); // throws ZeroDepError
caseSentence(['a', 'b', 'c']); // throws ZeroDepError

// booleans
caseSentence(true); // throws ZeroDepError
caseSentence(false); // throws ZeroDepError

// other
caseSentence(/^$\d{7}/g); // throws ZeroDepError
caseSentence(new Date()); // throws ZeroDepError
caseSentence(new Date('2022-02-24')); // throws ZeroDepError
caseSentence(new Set()); // throws ZeroDepError
caseSentence(new Set([1, 2, 3])); // throws ZeroDepError
caseSentence(new Map()); // throws ZeroDepError
caseSentence(new Map([['a', 1]])); // throws ZeroDepError
caseSentence(new Symbol()); // throws ZeroDepError
caseSentence(new Error()); // throws ZeroDepError
caseSentence(() => {}); // throws ZeroDepError

// nothing
caseSentence(null); // throws ZeroDepError
caseSentence(undefined); // throws ZeroDepError

Related Packages

The following @zerodep packages may be helpful or more appropriate for your specific case:

Advantages of @zerodep Packages

We help make source code more readable, more secure, faster to craft, less likely to have hidden defects, and easier to maintain.

  • Zero npm dependencies - completely eliminates all risk of supply-chain attacks, decreases node_modules folder size
  • Fully typed - typescript definitions are provided for every package for a better developer experience
  • Semantically named - package and method names are easy to grok, remember, use, and read
  • Documented - actually useful documentation with examples and helpful tips
  • Intelligently Packaged - multiple npm packages of different sizes available allowing a menu or a-la-carte composition of capabilities
  • 100% Tested - all methods and packages are fully unit tested
  • ESM & CJS - has both ecmascript modules and common javascript exports, both are fully tree-shakable
  • CDN Available - available on fast content delivery networks in UMD, CJS and ESM formats
  • FP Inspired - gently opinionated to encourage functional programming style for cleaner and more maintainable software
  • Predictably Versioned - semantically versioned for peace-of-mind upgrading, this includes changelogs
  • MIT Licensed - permissively licensed for maximum usability

Support

All @zerodep packages are built for the ES2020 specification. Should you need to support older environments you will need to add appropriate polyfills. All packages are tested on the following platforms/browsers:

Browsers

  • Chrome - last 2 major versions
  • Firefox - last 2 major versions
  • Safari - last 2 major versions
  • Edge - last 2 major versions
  • Android - last 2 major versions
  • iOS - last 2 major versions

Node

  • v16.x - Gallium LTS
  • v14.x - Fermium LTS

It is likely the package will work on other technologies and version, however development and testing effort is only spent on the above.

Semver

All @zerodep packages, including this one, adhere to Semantic Versioning practices:

  • major versions: correlates with breaking changes to one or more method signatures
  • minor versions: includes addition of new functionality or backwards-compatible software improvements
  • patch versions: are reserved for copy changes, documentation enhancements and bug fixes

The above said, a security best practice is to pin your software packages to specific versions and only upgrade to more recent releases after careful inspection of any CHANGELOG, release notes and all software changes.

Resources

License

MIT

1.0.1

2 years ago

1.0.0

2 years ago