"Hello" "hello there" => "Hello th...">

Dot-free title function with Ramda

When writing a capitalization function, it is trivial that:

"hello" => "Hello" "hello there" => "Hello there"

How to write it using dot style using Ramda JS?

https://en.wikipedia.org/wiki/Tacit_programming

+4
source share
4 answers

It would be like this:

const capitalize = R.compose(
    R.join(''),
    R.juxt([R.compose(R.toUpper, R.head), R.tail])
);

Demo (at ramdajs.com REPL).

And a minor modification to handle the nullvalues

const capitalize = R.compose(
    R.join(''),
    R.juxt([R.compose(R.toUpper, R.head), R.tail])
);

const capitalizeOrNull = R.ifElse(R.equals(null), R.identity, capitalize);
+7
source

You can partially apply replaceto a regex that runs toUpperfor the first character:

const capitalize = R.replace(/^./, R.toUpper);

+6
source

R.lens:

const char0 = R.lens(R.head, R.useWith(R.concat, [R.identity, R.tail]));

R.over(char0, R.toUpper, 'ramda');
// => 'Ramda'
+3

I put together some quick and dirty tests for everyone who is interested. It looks like @ lax4mike is the fastest answer provided (although the simpler, non-dotted str[0].toUpperCase() + str.slice(1)way is faster [and also not the one the OP was asking about, so debate]).

https://jsfiddle.net/960q1e31/ (you need to open the console and run the script to see the results)

0
source

Source: https://habr.com/ru/post/1657573/


All Articles