strdr4605 / mockingcase

node package that converts a string to mOcKiNgCaSe
https://npm.im/@strdr4605/mockingcase
ISC License
76 stars 23 forks source link

Add option that converts only specific letters #39

Closed strdr4605 closed 5 years ago

strdr4605 commented 5 years ago

Option object should have these fields: upper should be of type string or instanceof RegExp lower should be of type string or instanceof RegExp else

throw TypeError(`options.upper: Expected string or RegExp but got "${typeof upper}"`);

The RegExp will be created as:

if (options.upper) const upperRegExp = new RegExp(options.upper, 'gi');
if (options.lower) const lowerRegExp = new RegExp(options.lower, 'gi');

Possible solution:

Step 1: Convert the input with default behavior (even prostion: lowerCase; odd position: upperCase) or if any option is present that do it first:

Step 2:

return step1Input
.replace(lowerRegExp, toLowerCase).
.replace(upperRegExp, toUpperCase);

upper has a higher priority so convert letters that much that regex after conversion of lower

Expected:

mockingcase("Hello World", { upper: "worl" });
// "hElLo WORLd"
mockingcase("Hello World", { upper: /worl/ });
// "hElLo WORLd"

mockingcase("Hello World", { upper: "[worl]" });
// "hELLO WORLd"
mockingcase("Hello World", { upper: /[worl]/ });
// "hELLO WORLd"

mockingcase("Hello World", { upper: /[worl]/, firstUpper: true });
// "HELLO WORLd"

TO DO:

Status:

@cukejianya is working on this!

cukejianya commented 5 years ago

@strdr4605 Do you mind if I take this on?

strdr4605 commented 5 years ago

@cukejianya, you can work on this. Let me know if you need some help!

strdr4605 commented 5 years ago

@cukejianya, Wanted to ask your opinion about RexExp flags.

For every letter in the string is needed to check

letter.match(new RegExp(options.upper, 'g'))

i flag should be added along with g flag and then the regex will ignore case, so

new RegExp('a', 'gi')

will match for both a and A.

strdr4605 commented 5 years ago

@cukejianya, As I discussed with some people, requirements for this issue will be probably changed! :blush:

Idea: upper/lower should behave like String.prototype.replace after default convert

I will think again and change them in the next days. If you have some opinion, please write.

cukejianya commented 5 years ago

@strdr4605 When you say upper/lower should behave like String.prototype.replace, do you mean that mockingcase("Hello World", upper:"worl") should return "Hello WORLd" instead of "heLLO WORld"

strdr4605 commented 5 years ago

@cukejianya, The new idea is: step1: default convert "hElLo wOrLd" step2: "hElLo wOrLd".replace(new RegExp(upper, 'gi'), toUpperCase) Use case

mockingcase("Hello World", { upper: "worl" });
// step1: "hElLo wOrLd"
// step2: "hElLo WORLd"
mockingcase("Hello World", { upper: /worl/ });
// step1: "hElLo wOrLd"
// step2: "hElLo WORLd"

mockingcase("Hello World", { upper: "[worl]" });
// step1: "hElLo wOrLd"
// step2: "hELLO WORLd"
mockingcase("Hello World", { upper: /[worl]/ });
// step1: "hElLo wOrLd"
// step2: "hELLO WORLd"

If other options are present the order should be:

  1. onlyLetter: remove non letters
  2. random: random case
  3. lower: lower replace
  4. upper: upper replace
  5. firstUpper: Before returning make sure first character is converted to upperCase

P.S. Let me know if you understand new requirements! You can also add comments. If everything is ok I will update the description of the issue!! 😊

P.S.1 Also

if (typeof upper !== "string" || !(upper instanceof RegExp)) {
  throw TypeError(`options.upper: Expected string or RegExp but got "${typeof upper}"`);
}
cukejianya commented 5 years ago

Gotcha. So that means that I should ignore:

  1. If only upper is present, then convert other letters that don't much the regex to lower case

  2. If only lower is present, then convert other letters that don't much the regex to upper case

I should only convert letters that match regex. And for those that do not match, default to convert(input, (str, i) => i % 2 === 1) (Unless other options are passed). Am I correct?

strdr4605 commented 5 years ago

Yes, but I would suggest to first convert default or random or firstUpper and after go with lower/upper!