Coder Social home page Coder Social logo

supercharge / strings Goto Github PK

View Code? Open in Web Editor NEW
44.0 4.0 19.0 309 KB

String utilities for Node.js

Home Page: https://superchargejs.com/docs/strings

License: MIT License

JavaScript 50.04% TypeScript 49.96%
string utilities supercharge nodejs chainable-methods hacktoberfest hacktoberfest2021

strings's Introduction



Strings

String utilities for Node.js.


Installation · Docs · Usage



Latest Version Monthly downloads

Follow @marcuspoehls and @superchargejs for updates!


Introduction

The @supercharge/strings package provides chainable string utilities for Node.js and JavaScript. It’s a wrapper around JavaScript’s global String class providing a handful of useful methods, like .title(), .strip(), .camel(), and so on.

Installation

npm i @supercharge/strings

Docs

Find all the details for @supercharge/strings in the extensive Supercharge docs.

Usage

Using @supercharge/strings is pretty straightforward. Pass a string to the imported Function and chain your desired methods to transform to string value to your needs.

For example, you may want to trim a string and then title-case it:

import { Str } from '@supercharge/strings'
// or 
const { Str } = require('@supercharge/strings')

const title = Str('  Supercharge is sweet!').trim().title().get()

// title: "Supercharge Is Sweet!"

For every method in the chain that would return a string, the package returns an instance of iteself. This way, you can chain further methods. Call .get() to retrieve the actual JavaScript string.

Contributing

Do you miss a string function? We very much appreciate your contribution! Please send in a pull request 😊

  1. Create a fork
  2. Create your feature branch: git checkout -b my-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request 🚀

License

MIT © Supercharge


superchargejs.com  ·  GitHub @supercharge  ·  Twitter @superchargejs

strings's People

Contributors

aitorres avatar anthony-22 avatar dependabot-preview[bot] avatar erikmichelson avatar g-traub avatar jabbar-zoop avatar jeconias avatar larswaechter avatar marcuspoehls avatar mcvladthegoat avatar sprakash57 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

strings's Issues

Add "padLeft" method

Add a new .padLeft(length, pad) method that pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string.

Example

Str('Marcus').padLeft(10, '.').get()
// '....Marcus'

Add "prepend" method

Add a new .prepend(...values) method that prepends the given values onto the string:

Example

Str('awesome').prepend('Supercharge ', 'is ').get()

// 'Supercharge is awesome'

Add "limit" method

The str.limit(limit, end = '…') method returns the first limit characters and ends the limited string with end.

Example

Str('Supercharge').limit(5, ' ->').get()

// 'Super ->'

Add "endsWith" method

The str.endsWith(needle) method determines whether a string ends with the given needle.

Example

Str('Supercharge is awesome').endsWith('some')

// true

Str('Supercharge is awesome').endsWith('Super')

// false

Add "replace" method

Add a new .replace(search, replace) method that replaces the first occurrence of the given search value with the replacing value.

Example

Str('Supercharge is nice').replace('nice', 'sweet').get()

// 'Supercharge is sweet'

Add Method: equalsIgnoreCase

Hello, I think a useful method to add would be one that checks for equality while ignoring the character cases. This would be called something like equalsIgnoreCase

Some examples:

Str('SUPERCHARGE').equalsIgnoreCase('supercharge') //true
Str('SuPeRcHaRgE').equalsIgnoreCase('sUpErChArGe') //true
Str('SUPERCHARGE').equalsIgnoreCase('SLOWCHARGE') //false
Str('SuPeR').equalsIgnoreCase('sUpErChArGe') //false

If this sounds good I'd love to create a PR

Limit string by number of words

Is there a way to limit a string by the number of words? I tried a few ways to get at it.

Str("To be or not to be").words().limit(3).get()

I'm doing it right now with:

Str(text).words().slice(0,5).join(" ") + "...

Add "startsWith" method

The str.startsWith(needle) method determines whether a string starts with the given needle.

Example

Str('Supercharge').startsWith('Super')

// true

Str('Supercharge').startsWith('super')

// false

Add "append" method

Add a new .append(...values) method that appends the given values to the string:

Example

Str('Supercharge').append(' is', ' awesome').get()

// 'Supercharge is awesome'

Add "padRight" method

Add a new .padRight(length, pad) method that pads the current string with a given string (repeated, if needed) so that the resulting string reaches a given length. The padding is applied from the end of the current string.

Example

Str('Marcus').padRight(10, '.').get()
// 'Marcus....'

Add replaceLast Method

The search method returns the position of the first occurrence of a specified text in a string

@marcuspoehls can you assign this to me and add label as hactoberfest.

Add "finish" method

Add a new .finish(suffix) method that adds a single instance of the given suffix value to the end of the string if it doesn’t already ends with the given suffix.

Example

Str('https://github.com').finish('/').get()
// 'https://github.com/'

Str('https://github.com/').finish('/').get()
// 'https://github.com/'

Add "ucFirst" method

The str.ucFirst() method uppercases the first character of the string.

Example

Str('superCHARGE').ucFirst().get()

// 'SuperCHARGE'

Add method: isCamelCase

Hey! What about adding a new method to check whether a given string is written in CamelCase or not? This should work with a simple Regex like: /^[a-z]+([A-Z][a-z]+)+$/g.

Examples:

Str("helloWorld").isCamelCase() // true
Str("helloWOrld").isCamelCase() // false
Str("hello").isCamelCase() // false
Str("hellO").isCamelCase() // false
Str("Hello").isCamelCase() // false

If that's okay I would like to submit a PR :)

Add "start" method

Add a new .start(prefix) method that prepends a single instance of the given prefix value to the start of the string if it doesn’t already start with the given prefix.

Example

Str('repos/supercharge').start('/').get()
// '/repos/supercharge'

Str('/repos/supercharge').start('/').get()
// '/repos/supercharge'

Str.random(length) not working

I am using random function to generate a token as shown in the code below
const Str=require('@supercharge/strings); const token = Str.random(50);
but its giving me the error
TypeError: Str.random is not a function
The same code was working when i used it some months before.
Is random function is removed ?

Add "afterLast" method

Add a new .afterLast(delimiter) method that returns everything from the string after the last occurrence of the given delimiter. This method returns the entire string if the delimiter doesn’t exist in the string.

Example

Str('Supercharge-is-awesome').afterLast('-').get()

// 'awesome'

Add method: isUuid

Implement an isUuid method that returns true if the wrapped string value is a valid UUID. Returns false otherwise

Requirements

Add "trimStart" method

The str.trimStart() method removes all whitespace at the beginning of a string.
Example

Str('    Supercharge  ').trimStart().get()

// 'Supercharge  '

Add "concat" method

The str.concat(addon) method appends the given addon to the string.

Example

const str = Str('Supercharge').concat('is awesome').get()

// 'Supercharge is awesome'

Add "substr" method

The str.substr(start, length) method returns the substring of length length starting from position start.

Example

Str('Supercharge').substr(0, 5).get()

// 'Super'

Add "split" method

The str.split(separator) splits the string at the given separator and returns an array of all separated strings.

Example

Str('Supercharge-is-awesome!').split('-')

// [ 'Supercharge', 'is', 'awesome!' ]

Add "trimEnd" method

The str.trimEnd() method removes all whitespace at the end of a string.

Example

Str('  Supercharge     ').trimEnd().get()

// '  Supercharge'

Add "beforeLast" method

Add a new .beforeLast(delimiter) method that returns everything from the string before the last occurrence of the given delimiter. This method returns the entire string if the delimiter doesn’t exist in the string.

Example

Str('Supercharge-is-awesome').beforeLast('-').get()

// 'Supercharge-is'

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.