Utility functions for working with string case in Node.js.
See types_of_cases.md (docs/types_of_cases.md
) for examples and more documentation.
Also see the contents of the migration
folder for what might happen in
the future.
The advantage of using this package is that there are a few dependencies and string manipulation code can be seen in a single file (index.js).
Understanding what the transformations do is necessary and not just helpful.
Transformations should be documented.
At first, you might think you know what transformations are doing, but consider this:
- Do you remember the names of the cases you are converting from and to?
- Do you remember the difference between 'dashes' and 'hyphens'?
- What happens to all and each of the characters after the transformation? In 9 July 2020, the toStart() function in this package converted 'this is a test' to 'This Is A Test', but it also converted 'this is a tEST' to 'This Is A Test'. This meant that, not only was the first character of each word transformed, the others were too ("EST" to "est").
9 July 2020 Log: The toStart() function name and functionality will probably change.
This is not started or complete yet.
Indicates what you are converting from and what you are converting to in the same function name.
Get all of this done formally, because it's getting annoying even when
I am writing in C#. See types_of_cases.md (docs/types_of_cases.md
).
npm i case-utils --save
The names of these functions will most likely change in the future. For now, they kind of work.
- Bug 1: There would be a bug if you used something like
itemIDitemID
.
// Possible solution:
var test = 'itemIDitemID'
console.log(test.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase())
- Bug 2: Numbers in the text are not accounted for yet e.g.
TrainingDummy1
becomestraining-dummy1
when we want:training-dummy-1
. - Bug 3: Function name change to
camelOrPascalToKebab()
after case detection. - Enhancement:
hyphenate()
- 'thisIsATest' becomes: 'this-is-a-test'
- 'itemID' becomes: 'item-id' (NOT 'item-i-d')
- 'IsACollider' becomes: 'is-a-collider'
- 'PositionX' becomes: 'position-x'
- 'PlayerID' becomes: 'player-id'
- 'IDPlayer' becomes: 'id-player'
- 'ThisIsATest' becomes: 'this-is-a-test'
See the sample-project
folder for the example script.
Currently also does this for Pascal case.
CaseUtils.camelToKebab(string)
accepts a string as an argument and
returns a string.
const CaseUtils = require('case-utils');
console.log(CaseUtils.camelToKebab('thisIsATest')); // Returns `this-is-a-test`.
Converts 'this-is-a-test' to 'this is a test'.
CaseUtils.removeHyphens(string)
accepts a string as an argument and
returns a string.
const CaseUtils = require('case-utils');
console.log(CaseUtils.removeHyphens('this-is-a-test')); // Returns `this is a test`.
Converts 'this is a test' to 'This Is A Test'.
Also: 'this is a tEST' to 'This Is A Test'.
CaseUtils.toStart(string)
accepts a string as an argument and
returns a string.
Bug: Numbers in the text are not accounted for yet e.g. TrainingDummy1
const CaseUtils = require('case-utils');
console.log(CaseUtils.toStart('this is a test')); // Returns `This Is A Test`.