Code Monkey home page Code Monkey logo

Comments (10)

blakeembrey avatar blakeembrey commented on May 18, 2024

@LukeAskew Not at the moment, but if it was to be added, what's the proposed behaviour? I assume it's straightforward and we keep it as usual, but this should be a standard preservation flag so what would happen if the user, say, preserves whitespace? Maybe we just present it doesn't matter? Does this compliment or allow replacements to the current replacement group? What if the next feature request is to omit something?

Sorry, just laying out some thoughts to start thinking about this. Let me know what you think too. It's definitely possible to add, just need to think about the use cases that might arrise. What methods do you expect to preserve characters?

from change-case.

LukeAskew avatar LukeAskew commented on May 18, 2024

A "replace" option is interesting. I could see how preserve method could be an abstraction of replace.

Maybe something like this:

changeCase.titleCase('3.0 a simple test')
//=> "3 0 A Simple Test"

changeCase.titleCase('3.0 a simple test', {
  preserve: ['.']
})
//=> "3.0 A Simple Test"

changeCase.titleCase('3.0 a simple test', {
  replace: [
    ['.', '.'],
    [' ', '-']
  ]
})
//=> "3.0-A-Simple-Test"

Where preserve leverages the internal replace method, but with replaces with the same character.

from change-case.

blakeembrey avatar blakeembrey commented on May 18, 2024

Awesome thinking! So along those lines, I provide a "replace" argument at the moment on the base class but what we could do is make it a function. So you would do:

changeCase.titleCase('3.0 a simple test', function (char) {
  return char === '.' ? '.' : ' '
})

My other question is still around the current preserve behaviour. Right now I have a few "complex" regexps that support different characters and apply transformations. Should they be allowed to be overridden or should this be purely a addition instead of a subtraction feature?

from change-case.

LukeAskew avatar LukeAskew commented on May 18, 2024

The function example is interesting. Could it handle multiple characters? Like if I wanted to replace/preserve both . and -?

I always lean toward more configurable, but the trade-off is potential for more edge case bugs.

Can you provide an example of what the transformation override might look like?

from change-case.

blakeembrey avatar blakeembrey commented on May 18, 2024

Yes, it could. You'd just pass function (char) { return /\.\-/.test(char) ? char : ' ' }. I'm not sure on the transformation override, I can't actually work out what that was a reference to. Mostly I was thinking about characters you might want to omit that are currently included. Right now the code is simply https://github.com/blakeembrey/sentence-case/blob/master/sentence-case.js#L30-L36, and replacement could become a function instead of a string. However, I was thinking about what if you want to replace things I consider word characters? Maybe this can be more general and both parts can be overridden/extended? Things get a lot more complicated though since the more that's added the more potential it deviates a lot with what people want. Anyway, the function I think is easy and perfectly doable and doesn't break backward compatibility. What do you think?

from change-case.

anthonator avatar anthonator commented on May 18, 2024

Anything ever happen with this?

from change-case.

blakeembrey avatar blakeembrey commented on May 18, 2024

No, but welcome to a having a completed PR.

from change-case.

YounGoat avatar YounGoat commented on May 18, 2024

I donnot think it is not a good idea to make common styles more complicated to deal with small probability events. It is more rational to create a customised case style. E.g.

const leca = require('leca');
const mycase = new leca.Case({
    splitter: /[\s_\-]/,
    jointer: '-',
    wordFormatter: word => word.toLowerCase(),
});
mycase.reformat('3.0 a simple test');
// RETURN "3.0-a-simple-test"

See README of leca for details.

from change-case.

Truemedia avatar Truemedia commented on May 18, 2024

For anyone looking for a work around or looking to take a crack at a PR here is some code I use to get it working when normally my brackets get taken out when doing snakecase.

let charName = argv.char;
let charTag = changeCase.snakeCase(charName);
let preserve = ['(', ')'];
for (punc of preserve) {
    while (charTag.indexOf(punc) != charName.lastIndexOf(punc)) {
        let index = charName.indexOf(punc);
        charTag = charTag.substr(0, index) + punc + charTag.substr(index);
    }
}

from change-case.

Truemedia avatar Truemedia commented on May 18, 2024

Just for reference charName is the string var before changecase applied, and charTag is the string var after chage case applied.

from change-case.

Related Issues (20)

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.