npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

leca

v0.0.10

Published

Letter Case Transformer

Downloads

16

Readme

leca

Letter Case Transformer

build status of github.com/YounGoat/ecmascript.leca total downloads of leca leca's License latest version of leca

To define your own letter case style and to deal with text by it. Or, to deal with text with predefined letter case styles.

Predefined Case Styles

| predefined case | case name | example | remark | |:---------------------- |:--------- |:-------------- |:----------------- | | leca.bicaps* | BiCapitalization | AltaVista eBay | | | leca.camel | camelCase | missYou | | | leca.kebab | kebab-case | i-love-you | | | leca.lowercamel | lowerCamelCase | missYou | s.a. camelCase | leca.pascal | PascalCase | PostScript | | | leca.sentence | Sentence case | I love you | | | leca.snake | snake_case | i_love_you | | | leca.title* | Title Case | I Love Your | | | leca.uppercamel* | UpperCamelCase | PostScript | s.a. PascalCase |

  1. Case postfixed with asterisk * may be in dispute with its impletation in leca. Please BE CAREFUL by yourself when using them.
  2. s.a. = the same as
  3. Because of its arbitrariness and creation, it unable to parse a phrase in StUdLyCaPs.

Table of contents

Links

Get Started

Use predefined common case styles:

const leca = require('leca');

leca.camel.test('camelCase'); 
// RETURN true

leca.camle.test('camel case');
// RETURN false

leca.camel.parse('camelCase');
// RETURN [ "camel", "case" ];

leca.camel.format('Camel', 'CASE');
// RETURN "camelCase"

Or, create your own case style:

const leca = require('leca');
const myCase = new leca.Case({
  // Used to split a formatted string.
  splitter: /(?=[A-Z])/,

  // Used to transform a word on formatting.
  wordFormatter: (word, index) => {
    if (index == 0) {
      return word.toLowerCase();
    }
    else {
      return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
    }
  },

  // Used to transform a word on parsing.
  wordParser: (word) => {
    return /^[a-zA-Z]+$/.test(word) ? word.toLowerCase() : false;
  }
});
myCase.test('camelCase');       // RETURN true
myCase.test('camel case');      // RETURN false
myCase.parse('camelCase');      // RETURN [ "camel", "case" ]
myCase.format('Camel', 'CASE'); // RETURN "camelCase"

API

leca.Case

An instance of class leca.Case represents a case style.

  • class leca.Case(object options)
    Hereafter use <ci> to represent an instance of leca.Case. See below for details of options.

  • boolean <ci>.test(string text)
    Return true if text matches the <ci>. Otherwise return false.

  • string[] | false <ci>.parse(string text)
    Split text into words.
    false will be returned if text doesnot match the case (e.g. doesnot start with options.prefix).

  • string | false <ci>.format(string[] words | ...words)
    Combine the words together according to the case format.
    false will be returned if any word doesnot match the case (depends on options.wordFormatter).

  • string | false <ci>.reformat(string text)
    Parse the text and format the words according to the case's format rule.

Following options may be used in new leca.Case(options) to define a letter case:

  • options.prefix string OPTIONAL
    What matching texts start with.

  • options.postfix string OPTIONAL
    What matching texts end with.

  • options.jointer string | Function OPTIONAL DEFAULT ""
    Used in formatting.
    As a string, it will be put between every two words.
    As a function, it should conform to:

    /**
     * @param {number} [index]
     * @param {string} [left]
     * @param {string} [right]
     * @return {string}
     */
    function(index, left, right) {
    	// ...
    }
  • options.splitter string | RegExp OPTIONAL
    Used in parsing.

  • options.terms string[] OPTIONAL
    Words to be preserved with its original style.
    In other words, the terms will escape from the constrains of options.wordFormatter and options.wordParser.

  • options.wordFormatter Function OPTIONAL
    Used in formatting.
    It should conform to:

    /**
     * @param {string}  word
     * @param {number} [index]
     * @param {number} [length] Total count of words
     * @return {string}
     */
    function(word, index, length) {
    	// ...
    }
  • options.wordParser Function OPTIONAL
    Used in parsing.
    It should conform to:

    /**
     * @param {string}  word
     * @param {number} [index]
     * @param {number} [length] Total count of words
     * @return {string | false}
     */
    function(word, index, length) {
    	// ...
    }

Predefined Common Letter Cases

For the convenience of developers, the most frequently-used letter case styles are predefined. Developers may access a predefined case style in form of leca.<stylename>, e.g.

const leca = require('leca');

// Access the predefined case style named "camel".
const mycase = leca.camel;

// ATTENTION: All predefined style names are lowercase, and there are no 
// punctuations or word "case" in names. Please DO NOT take the following for 
// granted:
leca.Pascal         // undefined
leca.camelCase      // undefined
leca['kebab-case']  // undefined
leca.snake_case     // undefined

// For those who have some kind of compulsive disorder, or just only wanna make
// code more comprehensible, invoke `leca()` may be helpful:
leca('Pascal')     === leca.pascal
leca('camelCase')  === leca.camel
leca('kebab-case') === leca.kebab
leca('snake_case') === leca.snake

In addition, some (acctually only 1 so far) accompanying methods are offered for each predefined letter case:

  • Case leca.*.terms(string | string[] terms)
    Return case instance based on corresponding predfined leca.Case. e.g.
    leca.camelCase.terms(['HTTP', 'HTTPS']).parse('myHTTPClient');
    // RETURN [ "my", "HTTP", "client" ]

This is multi-cultural world, and a letter case style will be named differently in different occassions. With respect to more people, leca offers some alias for the foregoing predefined case style.

See the table at beginning of this document for available predefined case styles.

Examples

Developers can learn how to create own letter case by leca via unit tests and predefined common letter cases:

References