ramda-adjunct icon indicating copy to clipboard operation
ramda-adjunct copied to clipboard

String Manipulation Functions

Open Undistraction opened this issue 8 years ago • 20 comments

Ramda already has:

Under discussion:

1. Case

  • [ ] capitalize

2. Formatting

Proposed:

1. Case

  • [ ] toSnakecase alpha_beta
  • [ ] toKebabCase / dasherize alpha-beta
  • [ ] toCamelCase alphaBeta
  • [ ] to PascalCase / alias: toUpperCamelCase AlphaBeta
  • [ ] toTitleCase Alpha Beta / alias: toStartCase Ref: lodash.startCase
  • [ ] toDotCase alpha.beta
  • [ ] toMacroCase / alias: toScreamingSnakeCase ALPHA_BETA

2. Formatting

  • [ ] wrap() wrap("'")('alpha') -> 'alpha' / wrap('[', ']')('alpha') -> [alpha]

3. Whitespace

  • [ ] compressWhitespace Reduce all whitespace to a single space per section of whitespace
  • [ ] stripWhitespace Remove all whitespace
  • [ ] stripLinebreaks Remove linebreaks
  • [ ] dedent Ref: npm(https://www.npmjs.com/package/dedent)
  • [ ] wordWrap Ref: Rails Wrap lines to x chars

4. Inflection Ref: lodash-inflection, Ref node-inflection

Undistraction avatar Feb 20 '18 12:02 Undistraction

Love wrap for creating pipelines ;-)

But is it a good idea to name it wrap ? maybe wrapString ?

for the others, names looks good to me.

guillaumearm avatar Feb 20 '18 12:02 guillaumearm

Ref: Some of my own formatting utils for reference. Some are project-specific, but some are general.

Ref: Rails string utils

Undistraction avatar Feb 20 '18 12:02 Undistraction

@Undistraction dedent is a nice one too https://www.npmjs.com/package/dedent

Let's update only your first comment so that it contains all the information for taking actions

char0n avatar Feb 20 '18 12:02 char0n

Regarding toUpperCamelCase / toPascalCase

toPascalCase should be primary name and tuUpperCamelCase @alias.

char0n avatar Feb 20 '18 12:02 char0n

toStartCase Ref: lodash.pad -> toStartCase Ref: lodash.startCase

char0n avatar Feb 20 '18 12:02 char0n

toTitleCase with capitalize @alias

char0n avatar Feb 20 '18 12:02 char0n

dedent is a nice one too https://www.npmjs.com/package/dedent

Added

Regarding toUpperCamelCase / toPascalCase

How about toMacroCase / toScreamingSnakeCase?

oStartCase Ref: lodash.pad -> oStartCase Ref: lodash.startCase

Done

toTitleCase with capitalize @alias

These are two different functions - capitalise capitalises only the first letter of the first word toTitleCase capitalises the first letter of every word.

Undistraction avatar Feb 20 '18 13:02 Undistraction

How about toMacroCase / toScreamingSnakeCase?

Would go for toMacroCase as primary and toScreamingSnakeCase as @alias

These are two different functions - capitalise capitalises only the first letter of the first word toTitleCase capitalises the first letter of every word.

Ok does toCapitalCase and toTitleCase make sense ?

char0n avatar Feb 20 '18 13:02 char0n

Ok does toCapitalCase and toTitleCase make sense ?

I think they have different intents - the to...Case are transforming across the whole string, but capitalize will only ever change the first char.

We could go with: firstToUpper or uppercaseFirst or similar, but capitalize is consistent with lodash.

Undistraction avatar Feb 20 '18 13:02 Undistraction

Yeah agree capitalize is for sure better.

For kebab case and other cases we will need these utils to sanitize the input string (should be part of src/internal):

// canonicalDecompose :: String -> String
export const canonicalDecompose = invoker(1, 'normalize')('NFD');

// removes combining characters from decomposed form
// removeAccents :: String -> String
export const removeAccents = replace(/[\u0300-\u036f]/g, '');

char0n avatar Feb 20 '18 13:02 char0n

@char0n Lodash exposes removal of accents as deburr so probably worth adding.

Undistraction avatar Feb 20 '18 13:02 Undistraction

Cool so we'll combine these two into deburr

This is lodash deburr implementation. As it seems my utils solve only part of the problem. There are other problems to solve. But we should refrain from copying the from lodash to avoid licensing issues and do our own research. deburr is priority now because it's the base for lot of other string utility functions.

char0n avatar Feb 20 '18 13:02 char0n

Question: what input are we accepting for all the to...Case functions. Should they only work on a spaced string - alpha beta or should they work on each other - for example should toCamelCase work on alpha-beta and alpha.beta?

Undistraction avatar Feb 22 '18 08:02 Undistraction

I would expect them to work on each other. Lodash seems to agree.

char0n avatar Feb 22 '18 08:02 char0n

stripLinebreaks -- trim is often used for that, it strips all whitespace from beggining and end of the string.

What stripWhitespace would be supposed to do?

wrap should have fixed arity of 3. And it wouldn't have to be limited to strings :)

wojpawlik avatar Feb 24 '18 13:02 wojpawlik

@char0n You might be interested in a little lib I just published: https://github.com/Undistraction/dedent-preserving-indents. It works much like dedent, but preserves all indentation past the line with the least whitespace. Might be nice to expose as a utility through RA.

Undistraction avatar Feb 25 '18 16:02 Undistraction

@Undistraction yeah why not, but it will have to reintegrated and not used as dependency.

char0n avatar Feb 25 '18 16:02 char0n

@gingerplusplus I think you've failed to understand the purpose of this function.

Undistraction avatar Feb 25 '18 18:02 Undistraction

but it will have to reintegrated and not used as dependency

@char0n Yep. I'll open an issue for it and we can look at it somewhere down the line.

Undistraction avatar Feb 25 '18 18:02 Undistraction

Also upperFirst, truncate would be useful

damiangreen avatar Apr 01 '20 20:04 damiangreen