@trenskow/caseit 1.0.0 → 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/.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/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Copyright 2020 Kristian Trenskow
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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
 
@@ -17,20 +33,27 @@ You can convert from any format to any other format. Supported formats are.
17
33
  * `domain` - domain.case
18
34
  * `kebab` - kebab-case
19
35
  * `title` - Title Case
36
+ * `http` - Http-Case
20
37
 
21
- > Default format (if omitted) is `camel`.
38
+ > Default format (if omitted) is `camel` (because it is the Javascript default).
22
39
 
23
- # LICENSE
40
+ ## Detecting
24
41
 
25
- Copyright 2020 Kristian Trenskow
42
+ To detect the casing of a string do as the following example.
26
43
 
27
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
44
+ ````javascript
45
+ caseit.detect('MyCase'); // returns ['pascal']
46
+ caseit.detect('Hello'); // returns ['pascal', 'title', 'http']
47
+ ````
28
48
 
29
- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
49
+ ## Getting the words
30
50
 
31
- 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
51
+ You can also get the lowercase variants of all the words in a string by using the following example.
32
52
 
33
- 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
53
+ ````javascript
54
+ caseit.words('MyCase'); // returns ['my', 'case']
55
+ ````
34
56
 
35
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57
+ # LICENSE
36
58
 
59
+ 3-Clause BSD (see LICENSE).
package/index.js CHANGED
@@ -1,40 +1,55 @@
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
- const separators = {
6
- 'camel': '',
7
- 'pascal': '',
8
- 'snake': '_',
9
- 'domain': '.',
10
- 'kebab': '-',
11
- 'title': ' '
12
- };
13
-
14
- if (Object.keys(separators).indexOf(type) == -1) {
15
- 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 `camel`, `pascal`, `snake`, `domain`, `kebab`, `title`, `http`.');
16
19
  }
17
20
 
18
- const parts = input.split(/(?=[A-Z])|_|-| |\./)
21
+ const words = exports.words(input)
19
22
  .filter((key) => key.length)
20
23
  .map((key, idx) => {
21
24
  switch (type) {
22
25
  case 'camel':
23
26
  if (idx == 0) return key.toLowerCase();
24
- // falls through
27
+ // fallthrough
25
28
  case 'title':
26
- // falls through
29
+ // fallthrough
30
+ case 'http':
31
+ // fallthrough
27
32
  case 'pascal':
28
33
  return key.charAt(0).toUpperCase() + key.substring(1).toLowerCase();
29
34
  case 'domain':
30
- // falls through
35
+ // fallthrough
31
36
  case 'kebab':
32
- // falls through
37
+ // fallthrough
33
38
  case 'snake':
34
39
  return key.toLowerCase();
35
40
  }
36
41
  });
37
42
 
38
- return parts.join(separators[type]);
39
-
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);
40
55
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/caseit",
3
- "version": "1.0.0",
3
+ "version": "1.1.2",
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
  }