LarvenLLC / lingojs

A javascript library for formatting and manipulating text (strings).
https://larvenllc.github.io/lingojs/
MIT License
3 stars 0 forks source link
formatting javascript js language library lingo lingojs manipulating string string-manipulation strings text

lingojs

A javascript library for formatting and manipulating text (strings)

NPM JavaScript Style Guide npm

Node

Install

npm install --save lingojs

Or

yarn add lingojs

Usage

import lingo from "lingojs";

Or

const lingo = require("lingojs");

CDN

Install

<script src="https://cdn.jsdelivr.net/gh/LarvenLLC/lingojs@latest/lingo.min.js"></script>

Usage

lingo is global and accessible. Therefore, it can be used directly after importing the script via CDN. See CDN Usage Example.

Create

Create an instance of lingo. lingo takes any input (number/string/etc) and tries to convert it into a string.

var myLingo = lingo(1000);

var value = myLingo.value();
// 1000

var myLingo2 = lingo("With a number 1,000");

var value2 = myLingo2.value();
// With a number 1,000

Functions

value

Function to get the (real, final & primitive) value of the manipulated string.

lingo("Tanzania").value();

// Tanzania

add

Function to concatenate words (add a word with a space infront of the string).

lingo("Tanzania")
  .add("the")
  .add("land")
  .add("of")
  .add("Kilimanjaro")
  .add("and")
  .add("Zanzibar")
  .value();

// Tanzania the land of Kilimanjaro and Zanzibar
Parameter (Data Type) Explanation (Sample) Values Default
string (String) text to be added "Tanzania"
"234^12 USD"
``

convert

Function to convert a string from one naming convention to another.

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").convert(
  "sentence",
  "kebab"
);

// tanzania,-the-land-of-kilimanjaro-and-zanzibar
Parameter (Data Type) Explanation Values Default
from (String) current naming convention "sentence"
"pascal"
"snake"
"kebab"
"camel"
"title"
``
to (String) desired naming convention "sentence"
"pascal"
"snake"
"kebab"
"camel"
"title"
"sentence"

format

Function to format a string into a certain naming convention.

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").format();

// Tanzania, the land of kilimanjaro and zanzibar

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").format("kebab");

// tanzania,-the-land-of-kilimanjaro-and-zanzibar
Parameter (Data Type) Explanation Values Default
format (String) desired naming convention "sentence"
"pascal"
"snake"
"kebab"
"camel"
"title"
"sentence"

sentence

Function to format a string into the sentence naming convention (sentence case).

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").sentence();

// Tanzania, the land of kilimanjaro and zanzibar

pascal

Function to format a string into the pascal naming convention (pascal case).

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").pascal();

// Tanzania,TheLandOfKilimanjaroAndZanzibar

snake

Function to format a string into the snake naming convention (snake case).

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").snake();

// tanzania,_the_land_of_kilimanjaro_and_zanzibar

kebab | slug

Function to format a string into the kebab/slug naming convention (kebab/slug case).

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").kebab();
/* is same as */
lingo("Tanzania, the land of Kilimanjaro and Zanzibar").slug();

// tanzania,-the-land-of-kilimanjaro-and-zanzibar

camel

Function to format a string into the camel naming convention (camel case).

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").camel();

// tanzania,TheLandOfKilimanjaroAndZanzibar

title

Function to format a string into the title naming convention (title case).

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").title();

// Tanzania, The Land Of Kilimanjaro And Zanzibar

name

Function to format a string into desired human names convention.

lingo("John Doe Nairobi").name();

// John Doe Nairobi

lingo("John Doe Nairobi").name("fms");

// JDN

lingo("John Doe Nairobi").name("S, F M");

// Nairobi, John Doe

lingo("John Doe Nairobi").name("f.m.s.");

// J.D.N.

lingo("John Doe Nairobi").name("F M");

// John Doe

lingo("John Doe Nairobi").name("F m.s.");

// John D.N.

reverse

Function to reverse the characters in a string.

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").reverse();

// rabiznaZ dna orajnamiliK fo dnal eht ,ainaznaT

reverseWords

Function to reverse the words in a string.

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").reverseWords();

// Zanzibar and Kilimanjaro of land the Tanzania,

characters

Function that returns all the characters of a string, separated by commas.

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").characters();

// T,a,n,z,a,n,i,a,,,t,h,e,l,a,n,d,o,f,K,i,l,i,m,a,n,j,a,r,o,a,n,d,Z,a,n,z,i,b,a,r

words

Function that returns all the words of a string, separated by commas.

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").words();

// Tanzania,,the,land,of,Kilimanjaro,and,Zanzibar

count

Function that returns the number of characters in a string (minus spaces).

For full count with spaces use the 'length' property

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").count();

// 40

countWords

Function that returns the number of words in a string.

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").countWords();

// 7

chunk

Function that divides a string into segments of equal lengths.

lingo("A89f45eR8dd73Q1T").chunk();

// A89f-45eR-8dd7-3Q1T

lingo("A89f45eR8dd73Q1T").chunk(4, " ");

// A89f 45eR 8dd7 3Q1T

lingo("A89f45eR8dd73Q1T").chunk(2, "/");

// A8/9f/45/eR/8d/d7/3Q/1T
Parameter (Data Type) Explanation (Sample) Values Default
length (Number) length of each chunk 2
10
4
separator (String) separator character between chunks "/"
"#"
""
" "
"-"

wrap

Function that wraps the string by specified character(s).

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").wrap();

// (Tanzania, the land of Kilimanjaro and Zanzibar)

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").wrap("{}");

// {Tanzania, the land of Kilimanjaro and Zanzibar}

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").wrap("''");

// 'Tanzania, the land of Kilimanjaro and Zanzibar'

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").wrap('""');

// "Tanzania, the land of Kilimanjaro and Zanzibar"

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").wrap("#");

// #Tanzania, the land of Kilimanjaro and Zanzibar#
Parameter (Data Type) Explanation (Sample) Values Default
wrapper (String) character(s) to wrap text. max two characters: one at the start and one at the end of text "/"
"#"
"*"
"{}"
"()"

singleQuote

Function that wraps the string by single quotation marks.

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").singleQuote();

// 'Tanzania, the land of Kilimanjaro and Zanzibar'

quote | doubleQuote

Function that wraps the string by double quotation marks.

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").quote();
/* is same as */
lingo("Tanzania, the land of Kilimanjaro and Zanzibar").doubleQuote();

// "Tanzania, the land of Kilimanjaro and Zanzibar"

initials

Function that returns the initials of the string.

Accepts separator.

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").initials();

// T.T.L.O.K.A.Z

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").initials("-");

// T-T-L-O-K-A-Z

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").initials("");

// TTLOKAZ
Parameter (Data Type) Explanation (Sample) Values Default
separator (String) separator character between initials "/"
"-"
""
" "
"."

first

Function that returns the first n characters of the string.

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").first(8);

// Tanzania

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").first();

// T
Parameter (Data Type) Explanation (Sample) Values Default
length (Number) number of characters from the beginning of text to be returned 2
10
1

last

Function that returns the last n characters of the string.

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").last(8);

// Zanzibar

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").last();

// r
Parameter (Data Type) Explanation (Sample) Values Default
length (Number) number of characters from the end of text to be returned 2
10
1

truncate | prune

Function that truncates/clips the string to return the first n characters of the string.

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").truncate();

// Tanzania,

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").truncate(3);

// Tanzania, the land
Parameter (Data Type) Explanation (Sample) Values Default
length (Number) number of words from the beginning of text to be returned 2
10
1

ellipsis

Function that truncates the string to return the first n characters of the string with a set of dots (…) at the end.

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").ellipsis();

// Tanzania,...

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").ellipsis(3);

// Tanzania, the land...
Parameter (Data Type) Explanation (Sample) Values Default
length (Number) number of words from the beginning of text to be returned 2
10
1

decapitalize | lowerCase

Function to convert the whole string to lower case (small letters).

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").decapitalize();
/* is same as */
lingo("Tanzania, the land of Kilimanjaro and Zanzibar").lowerCase();

// tanzania, the land of kilimanjaro and zanzibar

capitalize | upperCase

Function to convert the whole string to upper case (capital letters).

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").capitalize();
/* is same as */
lingo("Tanzania, the land of Kilimanjaro and Zanzibar").upperCase();

// TANZANIA, THE LAND OF KILIMANJARO AND ZANZIBAR

set

Function that overrides the string with a new value.

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").set("Lingo");

// Lingo
Parameter (Data Type) Explanation (Sample) Values Default
string (String) new text to override string "Lingo"
"1000 Tanzanians"
``

Properties

length

Function to return the number of characters in a string.

lingo("Tanzania, the land of Kilimanjaro and Zanzibar").length;

// 46