@trenskow/caseit 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -4
- package/index.js +26 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,9 +5,25 @@ A small library for changing the case of a string.
|
|
|
5
5
|
|
|
6
6
|
# Usage
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
Import using the following example.
|
|
9
|
+
|
|
10
|
+
````javascript
|
|
11
|
+
const caseit = require('@trenskow/caseit');
|
|
12
|
+
````
|
|
13
|
+
|
|
14
|
+
– or
|
|
15
|
+
|
|
16
|
+
````javascript
|
|
17
|
+
import caseit from '@trenskow/caseit'
|
|
18
|
+
````
|
|
19
|
+
|
|
20
|
+
## Converting
|
|
21
|
+
|
|
22
|
+
To convert from one case type to another use the following example.
|
|
23
|
+
|
|
24
|
+
```javascript
|
|
25
|
+
const myCamelCase = caseit('my_camel_case', 'camel'); // return 'myCamelCase';
|
|
26
|
+
```
|
|
11
27
|
|
|
12
28
|
You can convert from any format to any other format. Supported formats are.
|
|
13
29
|
|
|
@@ -19,7 +35,24 @@ You can convert from any format to any other format. Supported formats are.
|
|
|
19
35
|
* `title` - Title Case
|
|
20
36
|
* `http` - Http-Case
|
|
21
37
|
|
|
22
|
-
> Default format (if omitted) is `camel
|
|
38
|
+
> Default format (if omitted) is `camel` (because it is the Javascript default).
|
|
39
|
+
|
|
40
|
+
## Detecting
|
|
41
|
+
|
|
42
|
+
To detect the casing of a string do as the following example.
|
|
43
|
+
|
|
44
|
+
````javascript
|
|
45
|
+
caseit.detect('MyCase'); // returns ['pascal']
|
|
46
|
+
caseit.detect('Hello'); // returns ['pascal', 'title', 'http']
|
|
47
|
+
````
|
|
48
|
+
|
|
49
|
+
## Getting the words
|
|
50
|
+
|
|
51
|
+
You can also get the lowercase variants of all the words in a string by using the following example.
|
|
52
|
+
|
|
53
|
+
````javascript
|
|
54
|
+
caseit.words('MyCase'); // returns ['my', 'case']
|
|
55
|
+
````
|
|
23
56
|
|
|
24
57
|
# LICENSE
|
|
25
58
|
|
package/index.js
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const separators = {
|
|
4
|
+
'camel': '',
|
|
5
|
+
'pascal': '',
|
|
6
|
+
'snake': '_',
|
|
7
|
+
'domain': '.',
|
|
8
|
+
'kebab': '-',
|
|
9
|
+
'title': ' ',
|
|
10
|
+
'http': '-'
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const supported = Object.keys(separators);
|
|
14
|
+
|
|
3
15
|
module.exports = exports = function(input, type = 'camel') {
|
|
4
16
|
|
|
5
|
-
|
|
6
|
-
'camel': '',
|
|
7
|
-
'pascal': '',
|
|
8
|
-
'snake': '_',
|
|
9
|
-
'domain': '.',
|
|
10
|
-
'kebab': '-',
|
|
11
|
-
'title': ' ',
|
|
12
|
-
'http': '-'
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
if (Object.keys(separators).indexOf(type) == -1) {
|
|
17
|
+
if (!supported.includes(type)) {
|
|
16
18
|
throw new TypeError('Type must either be `camel`, `pascal`, `snake`, `domain`, `kebab`, `title`, `http`.');
|
|
17
19
|
}
|
|
18
20
|
|
|
19
|
-
const
|
|
21
|
+
const words = exports.words(input)
|
|
20
22
|
.filter((key) => key.length)
|
|
21
23
|
.map((key, idx) => {
|
|
22
24
|
switch (type) {
|
|
@@ -38,6 +40,16 @@ module.exports = exports = function(input, type = 'camel') {
|
|
|
38
40
|
}
|
|
39
41
|
});
|
|
40
42
|
|
|
41
|
-
return
|
|
42
|
-
|
|
43
|
+
return words.join(separators[type]);
|
|
44
|
+
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
exports.words = function(input) {
|
|
48
|
+
return input.split(/(?=[A-Z])|_|-| |\./)
|
|
49
|
+
.map((word) => word.toLowerCase());
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
exports.detect = function(input) {
|
|
53
|
+
return supported
|
|
54
|
+
.filter((type) => exports(input, type) === input);
|
|
43
55
|
};
|