@trenskow/caseit 1.1.0 → 1.1.3

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.
Files changed (4) hide show
  1. package/.eslintrc.js +46 -47
  2. package/README.md +37 -4
  3. package/index.js +34 -20
  4. package/package.json +5 -2
package/.eslintrc.js CHANGED
@@ -1,49 +1,48 @@
1
1
  module.exports = {
2
- "env": {
3
- "es6": true,
4
- "node": true,
5
- "mocha": true
6
- },
7
- "parserOptions": {
8
- "ecmaVersion": 2017
9
- },
10
- "extends": "eslint:recommended",
11
- "rules": {
12
- "indent": [
13
- "error",
14
- "tab"
15
- ],
16
- "linebreak-style": [
17
- "error",
18
- "unix"
19
- ],
20
- "quotes": [
21
- "error",
22
- "single"
23
- ],
24
- "semi": [
25
- "error",
26
- "always"
27
- ],
28
- "no-console": [
29
- "error", {
30
- "allow": [
31
- "warn",
32
- "error",
33
- "info"
34
- ]
35
- }
36
- ],
37
- "no-unused-vars": [
38
- "error", {
39
- "argsIgnorePattern": "^_"
40
- }
41
- ],
42
- "no-empty": [
43
- "error", {
44
- "allowEmptyCatch": true
45
- }
46
- ],
47
- "require-atomic-updates": "off"
48
- }
2
+ 'env': {
3
+ 'es6': true,
4
+ 'node': true
5
+ },
6
+ 'parserOptions': {
7
+ 'ecmaVersion': 2017
8
+ },
9
+ 'extends': 'eslint:recommended',
10
+ 'rules': {
11
+ 'indent': [
12
+ 'error',
13
+ 'tab'
14
+ ],
15
+ 'linebreak-style': [
16
+ 'error',
17
+ 'unix'
18
+ ],
19
+ 'quotes': [
20
+ 'error',
21
+ 'single'
22
+ ],
23
+ 'semi': [
24
+ 'error',
25
+ 'always'
26
+ ],
27
+ 'no-console': [
28
+ 'error', {
29
+ 'allow': [
30
+ 'warn',
31
+ 'error',
32
+ 'info'
33
+ ]
34
+ }
35
+ ],
36
+ 'no-unused-vars': [
37
+ 'error', {
38
+ 'argsIgnorePattern': '^_'
39
+ }
40
+ ],
41
+ 'no-empty': [
42
+ 'error', {
43
+ 'allowEmptyCatch': true
44
+ }
45
+ ],
46
+ 'require-atomic-updates': 'off'
47
+ }
49
48
  };
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
- const caseit = require('caseit');
9
-
10
- const myCamelCase = caseit('my_camel_case', 'camel'); // myCamelCase;
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,43 +1,57 @@
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).sort();
14
+
3
15
  module.exports = exports = function(input, type = 'camel') {
4
16
 
5
- const separators = {
6
- 'camel': '',
7
- 'pascal': '',
8
- 'snake': '_',
9
- 'domain': '.',
10
- 'kebab': '-',
11
- 'title': ' ',
12
- 'http': '-'
13
- };
14
-
15
- if (Object.keys(separators).indexOf(type) == -1) {
16
- throw new TypeError('Type must either be `camel`, `pascal`, `snake`, `domain`, `kebab`, `title`.');
17
+ if (!supported.includes(type)) {
18
+ throw new TypeError(`Type must either be ${supported.slice(0, -1).join(', ')} or ${supported.slice(-1)[0]}.`);
17
19
  }
18
20
 
19
- const parts = input.split(/(?=[A-Z])|_|-| |\./)
21
+ const words = exports.words(input)
20
22
  .filter((key) => key.length)
21
23
  .map((key, idx) => {
22
24
  switch (type) {
23
25
  case 'camel':
24
26
  if (idx == 0) return key.toLowerCase();
25
- // falls through
27
+ // fallthrough
26
28
  case 'title':
27
- // falls through
29
+ // fallthrough
28
30
  case 'http':
29
- // falls through
31
+ // fallthrough
30
32
  case 'pascal':
31
33
  return key.charAt(0).toUpperCase() + key.substring(1).toLowerCase();
32
34
  case 'domain':
33
- // falls through
35
+ // fallthrough
34
36
  case 'kebab':
35
- // falls through
37
+ // fallthrough
36
38
  case 'snake':
37
39
  return key.toLowerCase();
38
40
  }
39
41
  });
40
42
 
41
- return parts.join(separators[type]);
42
-
43
+ return words.join(separators[type]);
44
+
43
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);
55
+ };
56
+
57
+ exports.supported = supported;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/caseit",
3
- "version": "1.1.0",
3
+ "version": "1.1.3",
4
4
  "description": "Small library for converting between different casing.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -20,5 +20,8 @@
20
20
  "bugs": {
21
21
  "url": "https://github.com/trenskow/caseit/issues"
22
22
  },
23
- "homepage": "https://github.com/trenskow/caseit#readme"
23
+ "homepage": "https://github.com/trenskow/caseit#readme",
24
+ "devDependencies": {
25
+ "eslint": "^8.2.0"
26
+ }
24
27
  }