Giter VIP home page Giter VIP logo

txttools-titlecase's Introduction

txttools-titlecase

Convert string content to title case with options.

Usage

There are two versions, one is the main file which is used like a regular npm package to be required, and the other which can be copied & pasted into any project as a plugin.

Main File (/titlecase.js)

Makes use of separate function, setOptions to allow for multiple option updates to the same instance, rather than creating a new instance for every set of options like the version below.

const tc = require('txttools-titlecase');

tc.setOptions('caps','php');

console.log(tc('"hello" world! this is not php.'));
  // Expected: "Hello" World! This Is Not PHP.

tc.setOptions({camel: 'JavaScript', lower: ['hello','world']});

console.log(tc('"hello" world! this is javascript, not php.'));
  // Expected: "hello" world! This Is JavaScript, Not PHP.

Global File (/src/titlecase.js)

Makes use of closures to allow for the same function to act as the option setter on declaration & the titlecase function itself upon subsequent calls.

/* File is included somewhere in the environment; directly paste, included in html, etc... */
let tc = titleCase({camel: 'JavaScript', lower: ['hello','world'], caps: 'php'});

console.log(tc('"hello" world! this is javascript, not php.'));
  // Expected: "hello" world! This Is JavaScript, Not PHP.

Options

  • lower
    • Words to appear in lowercase. Note that if the word appears after a stop punctuation (like a period, question mark etc.) or appears at the end of the string (as per titlecase publication rules) - this will be ignored.
  • caps
    • Words to appear in all-caps.
  • camel
    • Words which have mixed casing. Pass the word in the form of which you'd like it to appear to options and it will convert occurences of that word to that form. (eg: 'JavaScript')

If you only need to modify one of these options, you may pass it as a string rather than an object to setOptions (node version) or in the declaration (global version).

// Node
const tc = require('txttools-titlecase');
tc.setOptions('lower','iou');             // Valid
tc.setOptions('caps', ['js','php'])       // Also Valid

// Global
let tc = titleCase('caps', ['js','PHP']); // Valid

Note: It doesn't matter what case you pass to lower, caps options. It will work as expected either way. The only option whose casing matters when you set it is camel.

If you need to modify more than one at the same time, you can pass an object with each of the options as the key, and the word(s) as the values. (string|array of strings)

// Node
const tc = require('txttools-titlecase');
tc.setOptions({lower:'iou', caps: ['js','php']}); // Valid

// Global
let tc = titleCase(caps: ['js','PHP']);           // Also Valid

Dependencies

Project

  • None

Development

Package GitHub npm Docs
Chai chaijs/chai chai chaijs.com
Mocha mochajs/mocha mocha mochajs.org
Rewire jhnns/rewire rewire
Sinon sinonjs/sinon sinon sinonjs.org

txttools-titlecase's People

Contributors

augint avatar

Stargazers

 avatar

Watchers

 avatar  avatar

txttools-titlecase's Issues

Error in readme, and throws on non-ascii

The README says to call tc(...), but it should be tc.titleCase(...). Moreover this errors out:

const tc = require('txttools-titlecase');
console.log(tc.titleCase('hello'));
console.log(tc.titleCase("日本型排外主義: 在特会・外国人参政権・東アジア地政学"));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.