• Stars
    star
    170
  • Rank 222,654 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 13 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A collection of utility functions for working with strings in JavaScript in the browser or Node

Slang

A collection of utility functions for working with strings in JavaScript in the browser or CommonJS.

Node Installation

npm install slang

Annotated source code

The annotated source code for slang, generated by Docco, can be found here.

API

slang.isString

Returns whether input is a string

slang.isString('testing'); // true
slang.isString(543);       // false

slang.capitalize

Capitalizes the first character of a string

slang.capitalize('hello world!'); // "Hello world!"

slang.uncapitalize

Uncapitalizes the first character of a string

slang.uncapitalize('Hello world!); // "hello world!"

slang.capitalizeWords

Capitalizes each word in the string

slang.capitalizeWords('hello world!'); // "Hello World!"

slang.uncapitalizeWords

Uncapitalizes each word in the string

slang.uncapitalizeWords('Hello World!'); // "hello world!"

slang.isUpperCaseAt

Returns whether the character at the provided character index is upper case.

slang.isUpperCaseAt('Testing', 0); // true

slang.isLowerCaseAt

Returns whether the character at the provided character index is lower case.

slang.isLowerCaseAt('Testing', 1); // true

slang.swapcase

Inverts the case for each letter in the string

slang.swapcase('aaBBccDD'); // "AAbbCCdd"

slang.camelize

Converts a string of words seperated by dashes or spaces to camelCase

slang.camelize('hello world'); // "helloWorld"
slang.camelize('hello-world'); // "helloWorld"

slang.uncamelize

slang.uncamelize('helloWorld'); // "hello World"

slang.dasherize

Converts a string of words or a camelCased string into a series of words separated by a dash (-)

slang.dasherize('this is dashed'); // "this-is-dashed"

slang.repeat

Concatenates the string count times

slang.repeat('Ho! ', 3); // "Ho! Ho! Ho! "

slang.insert

Inserts string in input at index

slang.insert('this is cool!', 'really ', 8); // "this is really cool!"

slang.remove

Removes the characters between the start and end indexes

slang.remove('this is really cool!', 8, 15); // "this is cool!"

slang.chop

Removes the last character of input

slang.chop('hello'); // "hell"

slang.trim

Removes leading and trailing whitespace from input. Uses ES5's native trim is available.

slang.trim('hello '); // "hello"

slang.trimLeft

Removes the leading whitespace from input

slang.trimLeft(' hello '); // "hello "

slang.trimRight

Removes the trailing whitespace from input

slang.trimRight(' hello '); // " hello"

slang.truncate

Truncates input to args.limit or 10 and adds args.omission or "..."

slang.truncate('Lorem ipsum dolor sit amet.');                                           // 'Lorem ipsu...'
slang.truncate('Lorem ipsum dolor sit amet.', { limit: 5, omission: '...(read more)' }); // 'Lorem...(read more)'

slang.join

Joins an array into a humanized list. The last element is joined by "and" by default, but you can change it.

slang.join(['red', 'blue', 'green']);       // "red, blue and green"
slang.join(['red', 'blue', 'green'], 'or'); // "red, blue or green"

slang.humanize

Returns a humanized number with the correct suffix such as 1st, 2nd, 3rd or 4th.

slang.humanize(2);	 // "2nd"
slang.humanize(103); // "103rd"

slang.contains

Returns whether input contains string

slang.contains('hello world', 'world'); // true

slang.startsWith

Returns whether input starts with string

slang.startsWith('hello world', 'hello'); // true

slang.endsWith

Returns whether input ends with string

slang.endsWith('hello world', 'world'); // true

slang.isBlank

Returns whether input is empty or only contains whitespace

slang.isBlank(' '); // true
slang.isBlank('');  // true

slang.successor

Returns the successor to str. The successor is calculated by incrementing characters starting from the rightmost alphanumeric (or the rightmost character if there are no alphanumerics) in the string. Incrementing a digit always results in another digit, and incrementing a letter results in another letter of the same case.

If the increment generates a carry, the character to the left of it is incremented. This process repeats until there is no carry, adding an additional character if necessary.

slang.successor("abcd");      // "abce"
slang.successor("THX1138");   // "THX1139"
slang.successor("<<koala>>"); // "<<koalb>>"
slang.successor("1999zzz");   // "2000aaa"
slang.successor("ZZZ9999");   // "AAAA0000"

slang.guid

Returns a unique guid of the specified length, or 32 by default

slang.guid();   // "gE9FEtJknQVy3qkN9fxmTucYKTwFOno2"
slang.guid(15); // "b0apU4OH7ZgmEoU"

slang.addToPrototype

Adds the methods from the slang object to String.prototype. Does not add slang.guid, slang.humanize, slang.isString, slang.version, or itself.

slang.addToPrototype();
"test".capitalize(); // "Test"

slang.lang

The default language to be used with all inflection methods. Initially set to 'en' for English.

slang.pluralize

Pluralizes a string in the specified language or slang.lang by default

inflector.pluralize('man') // 'men'
inflector.pluralize('word', 'de') // non-default language

slang.singularize

Singularizes a string in the specified language or slang.lang by default

inflector.singularize('men') // 'man'
inflector.singularize('word', 'de') // non-default language

slang.Language

An object that describes a language's inflection rules. See source code for example usage.

slang.languages

An object holding slang.Language objects that describe various languages. English ('en') is provided by default and you can add additional languages by adding them to slang.languages. Then you can set the default language to this new language by setting slang.lang or just use your language by passing the language code as the second argument to slang.pluralize or slang.singularize.

// Create a language
var german = new slang.Language();
// Now add inflection rules
// ...

// Add language
slang.languages['de'] = german;

// Set default language
slang.lang = 'de';

License

slang is licensed under the MIT license.

More Repositories

1

regexgen

Generate regular expressions that match a set of strings
JavaScript
3,240
star
2

node-wkhtmltopdf

A wrapper for the wkhtmltopdf HTML to PDF converter using WebKit
JavaScript
602
star
3

dprint-node

A node API for the dprint TypeScript and JavaScript code formatter
Rust
449
star
4

reader

An API Compatible Replacement for Google Reader
JavaScript
355
star
5

glob-match

An extremely fast glob matching library in Rust.
Rust
276
star
6

tree-sitter-highlight

A syntax highlighter for Node powered by Tree Sitter. Written in Rust.
Rust
215
star
7

napi-wasm

An implementation of the napi API for WASM.
JavaScript
154
star
8

exif-reader

A small EXIF image metadata reader
JavaScript
145
star
9

protobuf-jsonschema

Compiles Protobuf IDL to JSON Schema
JavaScript
111
star
10

blob-stream

A Node-style writable stream for HTML5 Blobs
JavaScript
110
star
11

jpg-stream

A streaming JPEG encoder and decoder
C++
79
star
12

pi-christmas-lights

πŸŽ„ Sync Christmas lights to music with a Raspberry Pi and the Web Audio API
JavaScript
74
star
13

vue-hooks

Experiment to shim the React Hooks API in Vue 3
Vue
74
star
14

importer

Deprecated: File importing for CoffeeScript and JavaScript
CoffeeScript
60
star
15

apple-color-emoji

Replace emoji in strings with images from the Apple Color Emoji font
JavaScript
59
star
16

protobuf-validator

Validates objects against protocol buffer schemas
JavaScript
57
star
17

gif-stream

A streaming GIF encoder and decoder
JavaScript
52
star
18

svelte-hooks

HTML
51
star
19

atom-jade

Jade TextMate bundle converted for Atom
HTML
51
star
20

fontkit-demo

A variable fonts demo using fontkit
JavaScript
50
star
21

coffeepack

An implementation of the MessagePack serialization format in CoffeeScript for Node.js and the browser.
CoffeeScript
50
star
22

browserify-istanbul

A browserify transform for the istanbul code coverage tool
JavaScript
50
star
23

SNNeuralNet

A neural network library for Objective-C
Objective-C
41
star
24

react-aria-components

Prototype of a higher level component-based API on top of React Aria
JavaScript
34
star
25

pics

Ties together streaming image encoders and decoders with a nice API
JavaScript
34
star
26

contenteditable-emoji

A blog post/demo on how to support emoji cross browser inside contenteditable
JavaScript
34
star
27

to-ast

Converts JavaScript objects to equivalent ASTs
JavaScript
33
star
28

bundler-algorithm

Experimenting with a faster bundling algorithm
Rust
31
star
29

png-stream

A streaming PNG encoder and decoder
JavaScript
29
star
30

neuquant

JavaScript port of the NeuQuant image quantization algorithm
JavaScript
26
star
31

svgkit

An SVG renderer for PDFKit
JavaScript
23
star
32

color-generator

Generates colors based on the golden ratio
JavaScript
20
star
33

qunit-cli

A Node module that adds colorful CLI support for the QUnit testing framework
JavaScript
18
star
34

pixel-stream

A base transform stream class for image pixel data
JavaScript
18
star
35

glsl.js

A glsl to asm.js compiler
JavaScript
18
star
36

react-aria-tailwind

A set of components using React Aria and Tailwind
TypeScript
18
star
37

resizer-stream

A streaming image resizer
JavaScript
17
star
38

spellchecker.js

A spellchecker in CoffeeScript/JavaScript based on Hunspell
CoffeeScript
17
star
39

bmp.js

A BMP decoder in JS for the HTML5 Canvas element
CoffeeScript
14
star
40

zipcode

Node module to easily lookup city and state for a US zipcode
JavaScript
14
star
41

bittorrent.js

A work in progress BitTorrent client in JavaScript
CoffeeScript
13
star
42

standards

Ideas for HTML/CSS/JavaScript standards from the community
12
star
43

color-transform

Streaming image color space transforms
JavaScript
11
star
44

concat-frames

Concatenate a pixel-stream into an array of frames
JavaScript
11
star
45

rsp-gatsby

JavaScript
9
star
46

rsp-next

Test app using React Spectrum with Next.js
JavaScript
9
star
47

lzw-stream

A streaming LZW encoder and decoder in JavaScript
JavaScript
9
star
48

browserify-optional

A browserify transform that allows optional dependencies in try..catch blocks
JavaScript
9
star
49

jQuery-DB

A JavaScript data store queried by jQuery-like selectors
JavaScript
8
star
50

bench-napi

C++
8
star
51

wc-hooks

JavaScript
8
star
52

atom-stylus

Stylus TextMate bundle converted for Atom
CoffeeScript
6
star
53

snoozr

Snoozr is an app for sleeping
Objective-C
6
star
54

shared-object

Multi threaded shared memory objects in Node
C++
5
star
55

babel-plugin-transform-glob-import

Import multiple files at once with globs
JavaScript
5
star
56

node-coreaudio

Apple Audio Unit bindings for Node.js
C++
5
star
57

sourcemaps

C++
4
star
58

babel-plugin-transform-async-super

Transform super calls inside async class methods for Node 6
JavaScript
4
star
59

workspace-registry

Virtual NPM registry for Yarn workspaces
JavaScript
3
star
60

is.js

JavaScript function that returns whether an object is of any of the given types
JavaScript
3
star
61

node-impermium

A Node.js API client for impermium
JavaScript
3
star
62

worker-experiment

Experimental Rust-based worker farm for node
Rust
3
star
63

rust-threadlocal-bug

C
2
star
64

next-swc-helpers-repro

JavaScript
1
star
65

private-react

JavaScript
1
star
66

napi-repro

JavaScript
1
star
67

babel-plugin-multidimensional-array

Fast multidimensional typed arrays with a nice syntax
JavaScript
1
star