bitwalker / stringex

A string extensions library for node.js
https://www.npmjs.com/package/stringex
15 stars 4 forks source link

Feature request: Separate core lib stringex to be able to use native Javascript project #8

Closed tamvm closed 9 years ago

tamvm commented 10 years ago

Your idea in the project is pretty cool, however, it's better if you can extract util functions into separate Javascript and opensource it too.

Thanks a lot for your contribution!

bitwalker commented 9 years ago

Do you mean just utils.js? You'd be better off using lodash instead, since it's API is almost identical, and it's actively maintained. The utils contained within stringex are basically just there to facilitate the internal API.

Is there code outside of utils.js that you'd like to see extracted?

tamvm commented 9 years ago

"Do you mean just utils.js? " => Yes

I don't think lodash support converting from Unicode to ASCII.

bitwalker commented 9 years ago

Neither does the code in utils.js. The code in utils is mostly functional programming utilities which I use elsewhere in stringex. The API for stringex itself is very small, and is defined entirely in stringex.js. The functions it exposes are:

// Create a URI-friendly representation of the string
toUrl(string)

// Performs multiple text manipulations. Essentially a shortcut
// for typing them all. View source below to see which methods are run.
removeFormatting(string)

// Removes HTML tags from text.
stripHtmlTags(string, leaveWhitespace)

/* Converts HTML entities into the respective non-accented letters. Examples:
*
* "á".convert_accented_entities # => "a"
* "ç".convert_accented_entities # => "c"
* "è".convert_accented_entities # => "e"
* "î".convert_accented_entities # => "i"
* "ø".convert_accented_entities # => "o"
* "ü".convert_accented_entities # => "u"
*
* Note: This does not do any conversion of Unicode/Ascii accented-characters. For that
* functionality please use toASCII.*/
convertAccentedEntities(string)

// Converts HTML entities (taken from common Textile/RedCloth formattings) into plain text formats.
convertMiscEntities(string)

/* Converts various common plaintext characters to a more URI-friendly representation.
*
* Note: Because this method will convert any & symbols to the string "and",
* you should run any methods which convert HTML entities (convert_html_entities and convert_misc_entities)
* before running this method. */
convertMiscCharacters(string)

/* Replace runs of whitespace in string. Defaults to a single space but any replacement
* string may be specified as an argument.*/
replaceWhitespace(string, replace)

/* Removes specified character from the beginning and/or end of the string and then performs
* squeeze, condensing runs of the character within the string. */
collapse(string, character)

/* Returns string of random characters with a length matching the specified limit. Excludes 0
* to avoid confusion between 0 and O. */
randomChars(limit)

// Converts a string to it's pure ASCII representation
toASCII(string)

/* Returns a new string or RegExp (depending on which one was passed in as target).
* EXAMPLES:
* stringex.interpolate('yellow #{0}', 'moon') #=> 'yellow moon'
* stringex.interpolate(/^[#{0}]$/g, '\\w') #=> /^[\w]$/g
* stringex.interpolate('My #{0} is #{name}', 'name', {
* name: 'Paul'
* }) #=> 'My name is Paul'
* NOTES:
* 1.) The arguments object is used here to allow for an unlimited number of parameters.
* 2.) You can mix and match interpolation types (named, indexed), but in order to
* use named variables, you must pass in an object with those names as properties.
* Objects are ignored for indexing (they will only be used for replacing named variables)
* so the order of the parameters is important, with Objects being the only exception. */
interpolate(target, **args)

/* Returns a new string where runs of the same character that occur
* in this set are replaced by a single character. If no arguments are given,
* all runs of identical characters are replaced by a single character.
* Repeated whitespace will always be squeezed.
*
* EXAMPLES:
* stringex.squeeze('yellow moon') #=> "yelow mon"
* stringex.squeeze(' now is the', ' ') #=> " now is the"
* stringex.squeeze('putters shoot balls', 'm-z') #=> "puters shot balls"
*/
squeeze(string, replace)

I'm not sure I see the point of breaking out any of those things into another library. The stuff in utils.js is already replicated in both underscore.js and lodash.js, and are much more widely used and better maintained - there probably isn't any point in splitting that out on it's own (though you are certainly welcome to grab the code in utils.js for your own needs if you like).

tamvm commented 9 years ago

Thanks, I see your ideas. So I close this issue too, don't worry on it.