Skip to content

Commit

Permalink
Require Node.js 10 and drop xregexp dependency (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyfarrell authored and sindresorhus committed Jan 25, 2020
1 parent eda5b74 commit 7ac6111
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 41 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ language: node_js
node_js:
- '12'
- '10'
- '8'
- '6'
35 changes: 14 additions & 21 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
declare const decamelize: {
/**
Convert a camelized string into a lowercased one with a custom separator: `unicornRainbow` → `unicorn_rainbow`.
/**
Convert a camelized string into a lowercased one with a custom separator: `unicornRainbow` → `unicorn_rainbow`.
@param string - The camelcase string to decamelize.
@param separator - The separator to use to put in between the words from `string`. Default: `'_'`.
@param string - The camelcase string to decamelize.
@param separator - The separator to use to put in between the words from `string`. Default: `'_'`.
@example
```
import decamelize = require('decamelize');
@example
```
import decamelize = require('decamelize');
decamelize('unicornRainbow');
//=> 'unicorn_rainbow'
decamelize('unicornRainbow');
//=> 'unicorn_rainbow'
decamelize('unicornRainbow', '-');
//=> 'unicorn-rainbow'
```
*/
(string: string, separator?: string): string;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function decamelize(string: string, separator?: string): string;
// export = decamelize;
default: typeof decamelize;
};
decamelize('unicornRainbow', '-');
//=> 'unicorn-rainbow'
```
*/
declare function decamelize(string: string, separator?: string): string;

export = decamelize;
17 changes: 3 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
'use strict';
const xRegExp = require('xregexp');

const decamelize = (text, separator = '_') => {
module.exports = (text, separator = '_') => {
if (!(typeof text === 'string' && typeof separator === 'string')) {
throw new TypeError('The `text` and `separator` arguments should be of type `string`');
}

const regex1 = xRegExp('([\\p{Ll}\\d])(\\p{Lu})', 'g');
const regex2 = xRegExp('(\\p{Lu}+)(\\p{Lu}[\\p{Ll}\\d]+)', 'g');

return text
// TODO: Use this instead of `xregexp` when targeting Node.js 10:
// .replace(/([\p{Lowercase_Letter}\d])(\p{Uppercase_Letter})/gu, `$1${separator}$2`)
// .replace(/(\p{Lowercase_Letter}+)(\p{Uppercase_Letter}[\p{Lowercase_Letter}\d]+)/gu, `$1${separator}$2`)
.replace(regex1, `$1${separator}$2`)
.replace(regex2, `$1${separator}$2`)
.replace(/([\p{Lowercase_Letter}\d])(\p{Uppercase_Letter})/gu, `$1${separator}$2`)
.replace(/(\p{Uppercase_Letter}+)(\p{Uppercase_Letter}[\p{Lowercase_Letter}\d]+)/gu, `$1${separator}$2`)
.toLowerCase();
};

module.exports = decamelize;
// TODO: Remove this for the next major release
module.exports.default = decamelize;
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -32,9 +32,6 @@
"text",
"convert"
],
"dependencies": {
"xregexp": "^4.2.4"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
Expand Down

0 comments on commit 7ac6111

Please sign in to comment.