qsu 0.5.0 → 0.5.1

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 CHANGED
@@ -81,22 +81,23 @@ Utility to help process array type data.
81
81
  ## qsu.string
82
82
  Utility to help process string type data.
83
83
 
84
- | Method | Params | Description | Example |
85
- | --- |--------------------------------------------------------------------------------------------------------------------------------------| --- | --- |
86
- | .removeSpecialChar | <li>string **{String}**</li><li>withoutSpace **{Boolean}**</li> | Returns after removing all special characters, including spaces. | `removeSpecialChar('Hello, World!') // 'HelloWorld'` |
87
- | .removeNewLine | string **{String}** | Removes \n, \r characters or replaces them with specified characters. | `removeNewLine('ab\ncd') // 'abcd'`<br/>`removeNewLine('ab\r\ncd', '-') // 'ab-cd'` |
88
- | .capitalizeFirst | string **{String}** | Converts the first letter of the entire string to uppercase and returns. | `capitalizeFirst('abcd') // 'Abcd'` |
89
- | .count | <li>string **{String}**</li><li>search **{String}**</li> | Returns the number of times the second String character is contained in the first String argument. | `count('abcabc', 'a') // 2` |
90
- | .shuffle | <li>string **{String}**</li> | Randomly shuffles the received string and returns it. | `shuffle('abcdefg') // 'bgafced'` |
91
- | .createRandom | <li>length **{Number}**</li> | Returns a random String containing numbers or uppercase and lowercase letters of the given length. The default return length is 12. | `createRandom(5) // 'CHy2M'` |
92
- | .hideRandom | <li>str **{String}**</li><li>hideLength **{Number}**</li><li>hideStr **{String}**</li> | Replaces strings at random locations with a specified number of characters (default 1) with characters (default *). | `hideRandom('hello', 2, '#') // '#el#o'` |
93
- | .truncate | <li>str **{String}**</li><li>length **{Number}**</li><li>ellipsis **{String&#124;null}**</li> | Truncates a long string to a specified length, optionally appending an ellipsis after the string. | `truncate('hello', 3) // 'hel'`<br/>`truncate('hello', 2, '...') // 'he...'` |
94
- | .encrypt | <li>str **{String}**</li><li>secret **{String}**</li><li>algorithm **{String&#124;null}**</li><li>ivSize **{Number&#124;null}**</li> | Encrypt with the algorithm of your choice (algorithm default: aes-256-cbc, ivSize default: 16) using a string and a secret (secret). | `encrypt('test', 'secret-key')` |
95
- | .decrypt | <li>str **{String}**</li><li>secret **{String}**</li><li>algorithm **{String&#124;null}**</li> | Decrypt with the specified algorithm (default: `aes-256-cbc`) using a string and a secret (secret). | `decrypt('61ba43b65fc...', 'secret-key') // 'test'` |
96
- | .md5 | <li>str **{String}**</li> | Converts String data to md5 hash value and returns it. | `md5('test') // '098f6bcd4621d373cade4e832627b4f6'` |
97
- | .sha1 | <li>str **{String}**</li> | Converts String data to sha1 hash value and returns it. | `sha1('test') // 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'` |
84
+ | Method | Params | Description | Example |
85
+ | --- |--------------------------------------------------------------------------------------------------------------------------------------| --- |----------------------------------------------------------------------------------------|
86
+ | .removeSpecialChar | <li>string **{String}**</li><li>withoutSpace **{Boolean}**</li> | Returns after removing all special characters, including spaces. | `removeSpecialChar('Hello, World!') // 'HelloWorld'` |
87
+ | .removeNewLine | string **{String}** | Removes \n, \r characters or replaces them with specified characters. | `removeNewLine('ab\ncd') // 'abcd'`<br/>`removeNewLine('ab\r\ncd', '-') // 'ab-cd'` |
88
+ | .capitalizeFirst | string **{String}** | Converts the first letter of the entire string to uppercase and returns. | `capitalizeFirst('abcd') // 'Abcd'` |
89
+ | .capitalizeEachWords | <li>string **{String}**</li><li>naturally **{Boolean}**</li> | Converts every word with spaces to uppercase. If the naturally argument is true, only some special cases (such as prepositions) are kept lowercase. | `capitalizeEachWords('hello world') // 'Hello World'` |
90
+ | .count | <li>string **{String}**</li><li>search **{String}**</li> | Returns the number of times the second String character is contained in the first String argument. | `count('abcabc', 'a') // 2` |
91
+ | .shuffle | <li>string **{String}**</li> | Randomly shuffles the received string and returns it. | `shuffle('abcdefg') // 'bgafced'` |
92
+ | .createRandom | <li>length **{Number}**</li> | Returns a random String containing numbers or uppercase and lowercase letters of the given length. The default return length is 12. | `createRandom(5) // 'CHy2M'` |
93
+ | .hideRandom | <li>str **{String}**</li><li>hideLength **{Number}**</li><li>hideStr **{String}**</li> | Replaces strings at random locations with a specified number of characters (default 1) with characters (default *). | `hideRandom('hello', 2, '#') // '#el#o'` |
94
+ | .truncate | <li>str **{String}**</li><li>length **{Number}**</li><li>ellipsis **{String&#124;null}**</li> | Truncates a long string to a specified length, optionally appending an ellipsis after the string. | `truncate('hello', 3) // 'hel'`<br/>`truncate('hello', 2, '...') // 'he...'` |
95
+ | .encrypt | <li>str **{String}**</li><li>secret **{String}**</li><li>algorithm **{String&#124;null}**</li><li>ivSize **{Number&#124;null}**</li> | Encrypt with the algorithm of your choice (algorithm default: aes-256-cbc, ivSize default: 16) using a string and a secret (secret). | `encrypt('test', 'secret-key')` |
96
+ | .decrypt | <li>str **{String}**</li><li>secret **{String}**</li><li>algorithm **{String&#124;null}**</li> | Decrypt with the specified algorithm (default: `aes-256-cbc`) using a string and a secret (secret). | `decrypt('61ba43b65fc...', 'secret-key') // 'test'` |
97
+ | .md5 | <li>str **{String}**</li> | Converts String data to md5 hash value and returns it. | `md5('test') // '098f6bcd4621d373cade4e832627b4f6'` |
98
+ | .sha1 | <li>str **{String}**</li> | Converts String data to sha1 hash value and returns it. | `sha1('test') // 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'` |
98
99
  | .sha256 | <li>str **{String}**</li> | Converts String data to sha256 hash value and returns it. | `sha256('test') // '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'` |
99
- | .unique | <li>str **{String}**</li> | Remove duplicate characters from a given string and output only one. | `unique('aaabbbcc') // 'abc'` |
100
+ | .unique | <li>str **{String}**</li> | Remove duplicate characters from a given string and output only one. | `unique('aaabbbcc') // 'abc'` |
100
101
 
101
102
  ## qsu.math
102
103
  Utility for arithmetic on numbers.
@@ -110,15 +111,15 @@ Utility for arithmetic on numbers.
110
111
  ## qsu.verify
111
112
  Utility for data inspection.
112
113
 
113
- | Method | Params | Description | Example |
114
- | --- | --- | --- | --- |
115
- | .empty | data **{Any}** | Returns true if the passed data is empty or has a length of 0. | `empty([]) // true`<br/>`empty('') // true`<br/>`empty('abc') // false` |
116
- | .isUrl | <li>url **{String}**</li><li>withProtocol **{Boolean&#124;null}**</li><li>strict **{Boolean&#124;null}**</li> | Returns true if the given data is in the correct URL format. If withProtocol is true, it is automatically appended to the URL when the protocol does not exist. If strict is true, URLs without commas (.) return false. | `isUrl('google.com') // false`<br/>`isUrl('google.com', true) // true`<br/>`isUrl('https://google.com') // true` |
117
- | .contains | <li>string **{String}**</li><li>searchData **{Array&#124;String}** | Returns true if the first string argument contains the second argument "string" or "one or more of the strings listed in the array". | `contains('abc', 'a') // true`<br/>`contains('abc', 'd') // false`<br/>`contains('abc', ['a', 'd']) // true` |
118
- | .is2dArray | array **{Array}** | Returns true if the given array is a two-dimensional array. | `is2dArray([1]) // false`<br/>`is2dArray([[1], [2]) // true` |
119
- | .between | <li>value **{Number}**</li><li>range **{[min, max]}</li><li>inclusive **{Boolean&#124;null}**</li>** | Returns true if the first argument is in the range of the second argument ([min, max]). To allow the minimum and maximum values to be in the range, pass true for the third argument. | `between(10, [10, 20]) // false`<br/>`between(10, [10, 20], true) // true` |
120
- | .length | <li>data **{Any}**</li> | Returns the length of any type of data. If the argument value is null or undefined, 0 is returned. | `length('12345') // 5`<br/>`length([1, 2, 3]]) // 3` |
121
- | .isBotAgent | <li>userAgent **{String}**</li> | Analyze the user agent value to determine if it's a bot for a search engine. Returns true if it's a bot. | `isBotAgent('Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)') // true` |
114
+ | Method | Params | Description | Example |
115
+ | --- |-----------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| --- |
116
+ | .empty | data **{Any}** | Returns true if the passed data is empty or has a length of 0. | `empty([]) // true`<br/>`empty('') // true`<br/>`empty('abc') // false` |
117
+ | .isUrl | <li>url **{String}**</li><li>withProtocol **{Boolean&#124;null}**</li><li>strict **{Boolean&#124;null}**</li> | Returns true if the given data is in the correct URL format. If withProtocol is true, it is automatically appended to the URL when the protocol does not exist. If strict is true, URLs without commas (.) return false. | `isUrl('google.com') // false`<br/>`isUrl('google.com', true) // true`<br/>`isUrl('https://google.com') // true` |
118
+ | .contains | <li>string **{String}**</li><li>searchData **{Array&#124;String}**</li><li>exact **{Boolean}**</li> | Returns true if the first string argument contains the second argument "string" or "one or more of the strings listed in the array". If the exact value is true, it returns true only for an exact match. | `contains('abc', 'a') // true`<br/>`contains('abc', 'd') // false`<br/>`contains('abc', ['a', 'd']) // true` |
119
+ | .is2dArray | array **{Array}** | Returns true if the given array is a two-dimensional array. | `is2dArray([1]) // false`<br/>`is2dArray([[1], [2]) // true` |
120
+ | .between | <li>value **{Number}**</li><li>range **{[min, max]}</li><li>inclusive **{Boolean&#124;null}**</li>** | Returns true if the first argument is in the range of the second argument ([min, max]). To allow the minimum and maximum values to be in the range, pass true for the third argument. | `between(10, [10, 20]) // false`<br/>`between(10, [10, 20], true) // true` |
121
+ | .length | <li>data **{Any}**</li> | Returns the length of any type of data. If the argument value is null or undefined, 0 is returned. | `length('12345') // 5`<br/>`length([1, 2, 3]]) // 3` |
122
+ | .isBotAgent | <li>userAgent **{String}**</li> | Analyze the user agent value to determine if it's a bot for a search engine. Returns true if it's a bot. | `isBotAgent('Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)') // true` |
122
123
 
123
124
  ## qsu.format
124
125
  Utility that converts to Human-Readable String format.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qsu",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Quick and Simple Utility for javascript",
5
5
  "scripts": {
6
6
  "test": "npm run lint && mocha --require @babel/register --parallel test/*.spec.js",
package/string.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const crypto = require('crypto');
2
2
  const { rand } = require('./math');
3
+ const { contains } = require('./verify');
3
4
 
4
5
  const removeSpecialChar = (str, withoutSpace) => {
5
6
  if (!str || typeof str !== 'string') return str;
@@ -16,6 +17,20 @@ const capitalizeFirst = (str) => {
16
17
  return str.charAt(0).toUpperCase() + str.slice(1);
17
18
  };
18
19
 
20
+ const capitalizeEachWords = (str, naturally) => {
21
+ if (!str || typeof str !== 'string' || str.length < 1) return null;
22
+ const splitStr = str.trim().toLowerCase().split(' ');
23
+ for (let i = 0, iLen = splitStr.length; i < iLen; i += 1) {
24
+ if (!naturally || !contains(splitStr[i], [
25
+ 'in', 'on', 'the', 'at', 'and', 'or', 'of', 'for', 'to', 'that',
26
+ 'a', 'by', 'it', 'is', 'as', 'are', 'were', 'was', 'nor', 'an',
27
+ ])) {
28
+ splitStr[i] = capitalizeFirst(splitStr[i]);
29
+ }
30
+ }
31
+ return capitalizeFirst(splitStr.join(' '));
32
+ };
33
+
19
34
  const count = (str, search) => {
20
35
  if (!str || typeof str !== 'string' || !search || typeof search !== 'string') return 0;
21
36
  return (str.match(new RegExp(search, 'g')) || []).length;
@@ -115,6 +130,7 @@ module.exports = {
115
130
  removeSpecialChar,
116
131
  removeNewLine,
117
132
  capitalizeFirst,
133
+ capitalizeEachWords,
118
134
  count,
119
135
  shuffle,
120
136
  createRandom,
@@ -29,6 +29,13 @@ st`), 'test');
29
29
  done();
30
30
  });
31
31
 
32
+ it('capitalizeEachWords', (done) => {
33
+ assert.strictEqual(_.capitalizeEachWords('hello, world!'), 'Hello, World!');
34
+ assert.strictEqual(_.capitalizeEachWords('test'), 'Test');
35
+ assert.strictEqual(_.capitalizeEachWords('this is the test sentence.', true), 'This is the Test Sentence.');
36
+ done();
37
+ });
38
+
32
39
  it('count', (done) => {
33
40
  assert.strictEqual(_.count('hello', 'l'), 2);
34
41
  assert.strictEqual(_.count('abcdABCD', 'a'), 1);
@@ -35,6 +35,8 @@ describe('Verify', () => {
35
35
  assert.strictEqual(_.contains('12345', '10'), false);
36
36
  assert.strictEqual(_.contains('ABC', ['A', 'B', 'C']), true);
37
37
  assert.strictEqual(_.contains('ABC', ['D', 'E', 'F']), false);
38
+ assert.strictEqual(_.contains('ABC', ['AB', 'C'], true), false);
39
+ assert.strictEqual(_.contains('AB', ['AB', 'C', 'D'], true), true);
38
40
  done();
39
41
  });
40
42
 
package/verify.js CHANGED
@@ -25,12 +25,18 @@ const isUrl = (url, withProtocol = false, strict = false) => {
25
25
  return true;
26
26
  };
27
27
 
28
- const contains = (str, search) => {
28
+ const contains = (str, search, exact) => {
29
29
  if (!str || !search || (typeof str !== 'string' && typeof str !== 'object')
30
30
  || (typeof str === 'object' && !Array.isArray(str))) return false;
31
31
  if (typeof search === 'string') return str.indexOf(search) !== -1;
32
32
  for (let i = 0, iLen = search.length; i < iLen; i += 1) {
33
- if (str.indexOf(search[i]) !== -1) return true;
33
+ if (exact) {
34
+ if (str === search[i]) {
35
+ return true;
36
+ }
37
+ } else if (str.indexOf(search[i]) !== -1) {
38
+ return true;
39
+ }
34
40
  }
35
41
  return false;
36
42
  };