From 636f01b898268d2937c7c22d01edb4219bfab9d4 Mon Sep 17 00:00:00 2001 From: canderson Date: Sat, 27 Nov 2021 19:43:00 +1100 Subject: [PATCH] groupCAPS => group-caps (options.groupCaps) --- README.md | 33 ++++++++++++++++++++++++++++++++- index.js | 8 ++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8fdf418..1d4b6cc 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,37 @@ console.log(dashify('Foo----Bar', {condense: true})); //=> 'foo-bar' ``` +## options.groupCaps + +**Type:** `boolean` + +**Default**: `undefined` + +Treat multiple consecutive capitals as one word. + +```js +console.log(dashify('CamelCASERules', {groupCaps: true})); +//=> 'camel-case-rules' + +console.log(dashify('IndexID', {groupCaps: true})); +//=> 'index-id' + +console.log(dashify('CamelCASE', {groupCaps: true})); +//=> 'camel-case' + +console.log(dashify('aID', {groupCaps: true})); +//=> 'a-id' + +console.log(dashify('theIDForUSGovAndDOD', {groupCaps: true})); +//=> 'the-id-for-us-gov-and-dod' + +console.log(dashify('TheID-', {groupCaps: true})); +//=> 'the-id' + +console.log(dashify('-IDOne', {groupCaps: true})); +//=> 'id-one' +``` + ## About
@@ -121,4 +152,4 @@ Released under the [MIT License](LICENSE). *** -_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on November 19, 2018._ \ No newline at end of file +_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on November 19, 2018._ diff --git a/index.js b/index.js index a9826e2..a917f20 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,14 @@ module.exports = (str, options) => { if (typeof str !== 'string') throw new TypeError('expected a string'); + + if (options && options.groupCaps) + str = str + .replace(/([a-z])([A-Z]+)/g, (m, s1, s2) => s1 + ' ' + s2) + .replace(/([A-Z])([A-Z]+)([^a-zA-Z0-9]*)$/, (m, s1, s2, s3) => s1 + s2.toLowerCase() + s3) + .replace(/([A-Z]+)([A-Z][a-z])/g, + (m, s1, s2) => s1.toLowerCase() + ' ' + s2); + return str.trim() .replace(/([a-z])([A-Z])/g, '$1-$2') .replace(/\W/g, m => /[À-ž]/.test(m) ? m : '-')