plexis-js / plexis Goto Github PK
View Code? Open in Web Editor NEWLo-fi, powerful, community-driven string manipulation library.
License: MIT License
Lo-fi, powerful, community-driven string manipulation library.
License: MIT License
A caching mechanism is vital for string operations. Similar to lodash/memoize
the caching mechanism can significantly improve the dev and user's experience and performance.
@param {Function} [func] The function to have its output memoized.
@param {Function} [resolver] The function to resolve the caching mechanism.
@returns {Function} Returns the new memoized function.
Example usage
import cache from '@plexis/cache';
const toLower = x => x.toLowerCase()
const toLowerCache = cache(toLower);
// We are adding a dummy resolver function which returns the same cache every single time
const toLowerCacheWithResolver = cache(toLower, x => 'a');
toLowerCache('A'); // returns a;
toLowerCache('B'); // returns b;
toLowerCacheWithResolver('A'); // returns a;
toLowerCache('B'); // returns a !!;
Notes
The API should expose the caching layer per function, eg:
import cache from '@plexis/cache';
const toLower = x => x.toLowerCase()
const toLowerCache = cache(toLower);
toLowerCache('A'); // returns a;
toLowerCache('B'); // returns b;
console.log(toLowerCache.cache) // outputs the cache object
Hint
Map
works great for modern environments
Aliases
import cache from '@plexis/cache';
import {cache, memoize} from 'plexis';
For most cases, humans do not sort text as machines do.
["A friend", 'Boo', "another one"].sort()
// => ["A friend", "Boo", "another one"] 😬
Let's fix that :)
/**
* @description Compares input text as humans do.
* @param {String} first
* @param {String} second
* @returns {Integer}
* @example
* compare('', '') // => 0
* compare('A', 'a') // => 1
* compare('A', 'b') // => 1
* compare('A woman', 'a woman') // => 1
* compare('b', 'B') // => -1
* compare('C', 'b') // => -1
* compare('Qux20', 'Qux5') // => -1
* compare('Qux10', 'Qux11') // => 1
*/
Aliases
import compare from '@plexis/compare';
import {compare, compareLikeHuman, naturalCompare} from 'plexis';
For users consuming compose
(#1) we need to provide a more flexible way for operations.
_when
is used for checking and chaining within the composition for supercharged functions.
@param {Function|Boolean} [checking] A function or condition to check.
@param {Function} [func] The function to invoke.
@returns {Function} Returns the new composite function.
Example usage
import compose from '@plexis/compose';
import _when from '@plexis/when';
const inc = x => x + 1;
const add = (x, y) => x + y;
const setToZero = () => 0;
const compose = (...fns) => x => fns.reduce((r, f) => f(r), x);
const _when = (cond, f) => x => (cond(x) ? f(x) : x);
const ops = compose(inc, x => add(x, 2), _when(num => num > 0, compose(setToZero)));
ops(1); // returns 0
ops(100); // returns 0
ops(-100); // returns -97
Aliases
import _when from '@plexis/when';
import {_when} from 'plexis';
Converts the first character of the input text to upper case. If the second parameter is true, the method converts the rest of the subject to lower case.
@param {String} text
@param {Boolean} isLowercase
Example usage
import toCapitals from '@plexis/to-capitals';
toCapitals('foo Bar');
// => Foo Bar
toCapitals('fOO Bar');
// => FOO Bar
toCapitals('fOO Bar', true);
// => Foo bar
Aliases
import toCapitals from '@plexis/to-capitals';
import {toCapitals, toFirstCapital, capitalize} from 'plexis';
Splits input text into an array of characters.
toChars(); // => []
toChars(''); // => []
toChars(null); // => []
toChars('Hello world'); // => [ "H", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d" ]
toChars(1) // => ['1']
toChars(2e5) // => ["2", "0", "0", "0", "0", "0"]
Aliases
import toChars from '@plexis/to-chars';
import {toChars, chars} from 'plexis';
Checks whether the input text contains only alpha and digit characters.
Example usage
import isAlphaDigit from '@plexis/is-alphadigit';
isAlphaDigit('ABCD');
// => true
isAlphaDigit('Apollo11');
// => true
isEmpty('Hello world');
// => false
isEmpty('Hello,world');
// => false
isAlphaDigit('');
// => false
isEmpty(' ');
// => false
Aliases
import isAlphaDigit from '@plexis/is-alphadigit';
import {isAlphaDigit} from 'plexis';
Converts the input text to camel case.
Example usage
import toCamelCase from '@plexis/to-camel-case';
toCamelCase('Cool');
// => 'cool'
toCamelCase('cool mate');
// => 'coolMate'
toCamelCase('Hey how are you today?');
// => 'heyHowAreYouToday'
toCamelCase('PascalCase');
// => 'pascalCase'
toCamelCase('kebab-case');
// => kebabCase
Aliases
import toCamelCase from '@plexis/to-camel-case';
import {toCamelCase, camelize, camelCase} from 'plexis';
Some contributors are having yarn test
failures locally and on CI for packages/plexis/test/index.js
and they don't know how to remedy this issue. This issue is a result of not having the various compiled files in the correct locations resulting in various modules not being found. This is remedied by running yarn bootstrap
.
I propose adding a tip under the 'Pull Requests' heading:
💡 Tip: yarn test
failing for packages/plexis/test/index.js
? Try running yarn bootstrap
before running yarn test
again.
Since this is a simple tip and might not affect everyone, I think it makes sense to add this information as a tip in the README. It's proven effective already in #43 .
Since most Plexis functions are pure we can create composed functions using closures.
compose
works in a more functional way and provides users with flexibility, creating complex and tricky string operations. compose
actually invokes a series of functions.
@param {Function[]} [funcs] The functions to invoke.
@returns {Function} Returns the new composite function.
Example usage
import compose from '@plexis/compose';
const inc = x => x + 1;
const add = (x, y) => x + y;
const ops = compose(inc, x => add(x, 2));
ops(1); // returns 4;
ops(10); // returns 13;
Aliases
import compose from '@plexis/compose';
import {compose, _do} from 'plexis';
Swaps lowercase to uppercase and vice versa for the input text.
Example usage
import toSwapCase from '@plexis/to-swap-case';
toSwapCase('Hello WORLD');
// => 'hELLO world'
toSwapCase('123 toys');
// => '123 toys'
Aliases
import toSwapCase from '@plexis/to-swap-case';
import {toSwapCase, swapCase} from 'plexis';
Checks whether the input is a string containing only lowercase characters.
Example usage
import isLowercase from '@plexis/is-lowercase';
isLowercase('foo');
// => true
isLowercase('foo bar');
// => true
isLowercase('foo bar');
// => true
isLowercase('Bar');
// => false
Converts the input text to snake case.
Example usage
import toSnakeCase from '@plexis/to-snake-case';
toSnakeCase('Cool');
// => 'cool'
toSnakeCase('cool mate');
// => 'cool_mate'
toSnakeCase('Hey how are you today?');
// => 'hey_how_are_you_today'
toSnakeCase('PascalCase');
// => 'pascal_case'
toSnakeCase('kebab-case');
// => kebab_case
Aliases
import toSnakeCase from '@plexis/to-snake-case';
import {toSnakeCase, underscored} from 'plexis';
Pads the input text to a new length.
pad(); // => ''
pad('Hello', 1); // => 'Hello'
pad('Hello', 7); // => ' Hello '
pad('Hello', 7, '*'); // => '*Hello*'
pad('Hello', 10, '*'); // => '**Hello***'
pad('Hello', 10, '123'); // => '123Hello12'
Aliases
import pad from '@plexis/pad';
import {pad, lrpad} from 'plexis';
Pads the input text from right to a new length.
padRight ('Foo Bar', 1); // => 'Foo Bar'
padRight ('Foo Bar', 8); // => 'Foo Bar '
padRight ('Foo Bar', 8, '*'); // => 'Foo Bar*'
padRight ('Foo Bar', 10, '*'); // => 'Foo Bar***'
padRight ('Foo Bar', 10, '123'); // => 'Foo Bar123'
padRight ('Foo Bar', 9, '123'); // => 'Foo Bar12'
Aliases
import padRight from '@plexis/pad-right';
import {padRight, rpad} from 'plexis';
Pads the input text from left to a new length.
padLeft ('Foo Bar', 1); // => 'Foo Bar'
padLeft ('Foo Bar', 8); // => ' Foo Bar'
padLeft ('Foo Bar', 8, '*'); // => '*Foo Bar'
padLeft ('Foo Bar', 10, '*'); // => '***Foo Bar'
padLeft ('Foo Bar', 10, '123'); // => '123Foo Bar'
padLeft ('Foo Bar', 9, '123'); // => '12Foo Bar'
Aliases
import padLeft from '@plexis/pad-left';
import {padLeft, lpad} from 'plexis';
Swaps every single character with the next one in a given string.
Example usage
import toSwap from '@plexis/to-swap';
toSwap('ab');
// => 'ba'
toSwap('a b');
// => ' ab'
toSwap('abcd');
// => 'badc'
Aliases
import toSwap from '@plexis/to-swap';
import {toSwap, swap} from 'plexis';
This method calculates the Levenshtein Distance between two strings.
@param {String} [fromStr]
@param {String} [toStr]
@returns {Number} The Levenshtein distance
Example usage
import distance from '@plexis/distance';
distance('book', ''back') // => 2
distance('black', 'back') // => 1
distance('Foo', 'Bar') // => 3
Aliases
import distance from '@plexis/distance';
import {distance, calcLevenshtein, levenshtein} from 'plexis';
Repeats the input text number of times.
@param {String} [text] The input text.
@returns {Integer} times How many times shall we repeat the text, default to 1
@param {String} glue The glue for the output, default to ''
@returns {String} The repeated string
Example usage
import repeat from '@plexis/repeat';
repeat('ABCD');
// => true
repeat('Apollo11', 2);
// => 'Apollo11Apollo11'
repeat('x', 2);
// => 'xx'
repeat('x', 2, '+');
// => 'x+x'
repeat('xx', 2, '_');
// => 'xx_xx'
Aliases
import repeat from '@plexis/repeat';
import {repeat, multiply} from 'plexis';
Remove HTML tags from the input text.
Example usage
import withoutHTML from '@plexis/without-html';
withoutHTML('Hello world');
// => 'Hello world'
withoutHTML('<p>Hello</p> world')
// => ' world'
withoutHTML('<p Hello world')
// => '<p Hello world'
withoutHTML('<p id="">Hello world</p>')
// => ''
withoutHTML('<p id="" Hello world</p> <b></b> from the underground ')
// => ' from the underground'
Aliases
import withoutHTML from '@plexis/without-html';
import {withoutHTML, removeHTML} from 'plexis';
Checks whether the input is a string containing only uppercase characters.
Example usage
import isUpperCase️ from '@plexis/is-uppercase';
isUpperCase️('F');
// => true
isUpperCase️('FOO');
// => true
isUpperCase️('FOO BAR.');
// => true
isUpperCase️('BAr');
// => false
Splits the input text into an array of chunks.
@param {String} text The input String
@param {Integer} chunkSize The size of each chunk, default to 1
Example usage
import toChunks from '@plexis/to-chunks';
toChunks('Hello world');
// => ['H', 'e', 'l', 'l', 'o']
toChunks(' ');
// => [' ', ' ', ' ']
toChunks('1234567', 2);
// => ['12', '34', '56', '7']
toChunks('1234567', 6);
// => ['123456', '7']
Aliases
import toChunks from '@plexis/to-chunks';
import {toChunks, chop} from 'plexis';
A method that transforms text according to Turabian / Chicago Style format.
@param {String} [txt] The text to transform
@returns {String} Returns text transformed according to the The Chicago Manual of Style.
Example usage
import toChicago from '@plexis/to-chicago';
toChicago('This is a title'); // returns 'This Is a Title'
toChicago('in Turabian shows a sample title page for a class paper.'); // returns 'In Turabian Shows a Sample Title Page for a Class Paper.'
toChicago('camelCase PascalCase snake_case and kebab-case'); // returns 'Camelcase Pascalcase Snake_case and Kebab-Case'
Aliases
import toChicago from '@plexis/to-chicago';
import {toTurabian, toChicagoManualStyle, toChicagoStyle, toChicagoTitle } from 'plexis'
Checks whether the input text starts with the given pattern.
@param {String} text
@param {String|Regex} pattern
@param {Number} startingPosition, optional
Example usage
import startsWith from '@plexis/starts-with';
startsWith('This is me', 'This is');
// => true
startsWith('This is me', 'his is', 1);
// => true
startsWith('This is me', 'is is', 2);
// => true
startsWith('This is me', 'This is', 1);
// => false
Converts the input text to pascal case.
Example usage
import toPascalCase from '@plexis/to-pascal-case';
toPascalCase('Cool');
// => 'Cool'
toPascalCase('cool mate');
// => 'CoolMate'
toPascalCase('Hey how are you today?');
// => 'HeyHowAreYouToday'
toPascalCase('camelCase');
// => 'CamelCase'
toPascalCase('kebab-case');
// => KebabCase
Aliases
import toPascalCase from '@plexis/to-pascal-case';
import {toPascalCase, classify} from 'plexis';
Converts the input string from camelCase, PascalCase, kebab-case or snake_case to a more human-readable format.
The method also trims any whitespace and removes repeated whitespace from the input text.
Example usage
import toHuman from '@plexis/to-human';
toHuman('foo');
// => 'Foo'
toHuman('foo bar');
// => 'Foo bar'
toHuman(' foo-bar baz quxQuux corge_grault GarplyWaldo');
// => 'Foo bar baz qux quux corge grault garply waldo'
toHuman(' foo-bar-baz qux quux');
// => 'Foo bar baz qux quux'
Aliases
import toHuman from '@plexis/to-human';
import {toHuman, humanize} from 'plexis';
Converts the input text to its plural form.
Optional param: Count.
Optional param: Custom plural form (must also provide a count).
Example usage
import toPlural from '@plexis/to-plural';
toPlural('example');
// => 'examples';
toPlural('example', 10);
// => 'examples';
toPlural('example', 1);
// => 'example';
toPlural('example', 'examplez', 10);
// => 'examplez';
toPlural('example', 'examplez', 1);
// => 'example';
Aliases
import toPlural from '@plexis/to-plural';
import {toPlural, pluralize} from 'plexis';
Checks whether the input text ends with the given pattern.
@param {String} text
@param {String|Regex} pattern
@param {Number} startingPosition, optional
Example usage
import endsWith from '@plexis/ends-with';
const txt = 'This is me';
endsWith(txt, 'is me');
// => true
endsWith(txt, 'is m', txt.length - 1);
// => true
endsWith(txt, 'is m', txt.length);
// => false
endsWith(txt, 'Foo');
// => false
Checks whether the input text is empty.
Example usage
import isEmpty from '@plexis/is-empty';
isEmpty('');
// => true
isEmpty(' ');
// => true
isEmpty(' b ');
// => false
Aliases
import isEmpty from '@plexis/is-empty';
import {isEmpty} from 'plexis';
Checks whether the input text contains only digit characters.
Example usage
import isDigit from '@plexis/is-digit';
isDigit('1');
// => true
isDigit('2');
// => true
isEmpty('0xFF');
// => true
isEmpty('5e-2');
// => true
isDigit('0.5e2');
// => true
isEmpty(' ');
// => false
isEmpty('A');
// => false
isEmpty(',.');
// => false
isEmpty('Hello world!');
// => false
isEmpty('Ελληνικά');
// => false
Aliases
import isDigit from '@plexis/is-digit';
import {isDigit} from 'plexis';
Checks whether the input text contains only alpha characters.
Example usage
import isAlpha from '@plexis/is-alpha';
isAlpha('ABCD');
// => true
isAlpha('Apollo11');
// => false
isEmpty('1');
// => false
isEmpty('Hello,world');
// => false
isAlpha('');
// => false
isEmpty(' ');
// => false
Aliases
import isAlpha from '@plexis/is-alpha';
import {isAlpha} from 'plexis';
Checks whether the input is a string primitive type.
Example usage
import isString from '@plexis/is-string';
isString('Foo');
// => true
isString(1);
// => false
isString(null);
// => false
Checks whether the input is a string representation of a numeric value.
Example usage
import isNumeric from '@plexis/is-numeric';
isNumeric('1000');
// => true
isNumeric('Infinity');
// => true
isNumeric('-100.4');
// => true
isNumeric('1E+2');
// => true
isNumeric('0xFF');
// => true
isNumeric('-');
// => false
isNumeric('one');
// => false
Extracts the first length characters from the input text.
head(); // => ''
head('Hello'); // => 'H'
head('Hello', 2); // => 'He'
head('Hello', 100); // => 'Hello'
Aliases
import head from '@plexis/head';
import {head, first, popFirst} from 'plexis';
Extracts the last length characters from the input text.
tail(); // => ''
tail('Hello'); // => 'o'
tail('Hello', 2); // => 'lo'
tail('Hello', 100); // => 'Hello'
Aliases
import tail from '@plexis/tail';
import {tail, rest, last, pop} from 'plexis';
I'm resolving it my self.
Converts the first character of the input text to lowercase. If the second parameter is true, the method converts the rest of the subject to lower case.
@param {String} text
@param {Boolean} isLowercase
Example usage
import toLower from '@plexis/to-lower';
toLower('foo Bar');
// => foo Bar
toLower('FOO Bar');
// => fOO Bar
toLower('fOO Bar', true);
// => foo bar
Aliases
import toLower from '@plexis/to-lower';
import {toLower, toFirstLower, decapitalize } from 'plexis';
Remove leading whitespace from each line in a string
Example usage
import withoutIndent from '@plexis/without-indent';
withoutIndent('Hello world');
// => 'Hello world'
withoutIndent('\nHello world')
// =>
//'
// Hello world'
withoutIndent('\t\t\t\tHello world')
// => 'Hello world'
withoutIndent('\t\t\t\tHello \nworld')
// => '
// Hello
// world'
withoutIndent('\t\t\t\tHello\tdear \nworld')
// => ' Hello dear
// world'
Aliases
import withoutIndent from '@plexis/without-indent';
import {withoutIndent, removeIndent, dedent} from 'plexis';
Returns the number of words contained in the input text.
countWords(); // => 0
countWords(''); // => 0
countWords(' ') // => 0
countWords('Hello World') // => 2
countWords('Hello World !!') // => 2
Aliases
import countWords from '@plexis/count-words';
import {countWords} from 'plexis';
Return the number of characters in the input text.
An optional matcher can be passed as a validator for input characters.
count(); // => 0
count(''); // => 0
count('Hello') // => 5
count('Hello ') // => 8
count('Hello ', char => char !== ' ') // => 5
Aliases
import count from '@plexis/count';
import {count} from 'plexis';
Hello i have pretty much followed the instructions given inside the README page but my pull request has faced this error message :
ci/circleci: test — Your tests failed on CircleCI
and there was no explanation for what to do to satisfy its conditions!
Checks if the search is part of the input string in the given position.
includes(); // => false
includes(''); // => false
includes('', ''); // => true
includes('Foo', 'Foo'); // => true
includes('Foo', 'Foo'); // => true
includes('Foo', 'Foo', 2); // => false
includes('Foo', 'oo'); // => true
includes('Foo', 'Oo'); // => false
Aliases
import includes from '@plexis/includes';
import {includes, has} from 'plexis';
Inserts into the input text a string at specified position.
insert(); // => ''
insert('dg'); // => 'dg'
insert('dg', o); // => 'dgo'
insert('dg', o, 1); // => 'dog'
insert('dg', o, 100); // => 'dgo'
insert('dg', o, -100); // => 'dgo'
insert('dg', o, 0); // => 'odg'
Aliases
import insert from '@plexis/insert';
import {insert} from 'plexis';
Truncates the input text to the desired length.
Example usage
import toEllipsis from '@plexis/to-ellipsis';
toEllipsis('foo');
// => 'foo'
toEllipsis('foo', 1);
// => '...'
toEllipsis('foo', 3);
// => 'foo'
toEllipsis('As Gregor Samsa awoke one morning from uneasy dreams he found himself transformed in his bed into a monstrous vermin.', 20);
// => 'As Gregor Samsa a...'
toEllipsis('In a hole in the ground there lived a hobbit.', Infinity)
// => 'In a hole in the ground there lived a hobbit.'
toEllipsis('Last night I dreamt I went to Manderley again.', 5);
// => Last...
Aliases
import toEllipsis from '@plexis/to-ellipsis';
import {toEllipsis, truncate, fit} from 'plexis';
Converts the input text to kebab case.
Example usage
import toKebabCase from '@plexis/to-kebab-case';
toKebabCase('Cool');
// => 'cool'
toKebabCase('cool mate');
// => 'cool-mate'
toKebabCase('Hey how are you today?');
// => 'hey-how-are-you-today'
toKebabCase('camelCase');
// => 'camel-case'
toKebabCase('PascalCase');
// => pascal-case
Aliases
import toKebabCase from '@plexis/to-kebab-case';
import {toKebabCase, dasherize, slugify} from 'plexis';
I noticed this weird usage of backslashes on the isUpperCase
tests on the simple Windows 7 terminal:
PASS packages/toTitle/test/index.js
PASS packages\isUpperCase?\test\index.js
PASS packages/toPred/test/index.js
PASS packages/startsWith/test/index.js
PASS packages/distance/test/index.js
PASS packages/withoutDiacritics/test/index.js
PASS packages/toLower/test/index.js
PASS packages/compose/test/index.js
PASS packages/isLowerCase/test/index.js
PASS packages/toChicago/test/index.js
PASS packages/cache/test/index.js
PASS packages/toSucc/test/index.js
PASS packages/repeat/test/index.js
PASS packages/plexis/test/index.js
PASS packages/isString/test/index.js
Turns out that the directory must contain some sort of invisible character which causes this. Another way to figure this out is by attempting to enter the directory without autocomplete:
E:\dev\projects\plexis>ls -al packages\isUpperCase <- here I just typed in "isUpperCase"
ls: cannot access 'packages\isUpperCase': No such file or directory
E:\dev\projects\plexis>ls -al packages\isUpperCase️ <- here I typed in "isUp" and pressed tab.
total 10
drwxr-xr-x 1 jc 197121 0 Oct 23 21:07 .
drwxr-xr-x 1 jc 197121 0 Oct 23 21:07 ..
drwxr-xr-x 1 jc 197121 0 Oct 23 21:07 dist
-rw-r--r-- 1 jc 197121 481 Oct 23 21:07 package.json
-rw-r--r-- 1 jc 197121 434 Oct 23 21:07 README.md
drwxr-xr-x 1 jc 197121 0 Oct 23 21:07 src
drwxr-xr-x 1 jc 197121 0 Oct 23 21:07 test
Takes the input text and converts HTML special characters to their entity equivalents.
Example usage
import escapeHTML from '@plexis/escapeHTML';
escapeHTML('ABCD');
// => 'ABCD'
escapeHtml('<3')
// => '<3'
escapeHtml('<p>This is cool</p>')
// => '<p>This is cool</p>'
Aliases
import escapeHTML from '@plexis/escape-html';
import {escapeHTML} from 'plexis';
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.