Skip to content

Commit 49ed2db

Browse files
authored
Merge pull request #40 from SecJS/feat/len-string-and-is
feat: implement String and Is class and new methods to Parser
2 parents bfe0ace + 0b46973 commit 49ed2db

32 files changed

+10933
-318
lines changed

README.md

Lines changed: 149 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ await existentFile.move()
8888
await existentFile.remove()
8989
await existentFile.create()
9090
await existentFile.getContent()
91+
92+
// You can use safeRemove method to delete the file without any exception if it does no exists
93+
94+
await File.safeRemove(existentFile.path)
9195
```
9296

9397
---
@@ -145,6 +149,93 @@ await existentFolder.copy()
145149
await existentFolder.move()
146150
await existentFolder.remove()
147151
await existentFolder.create()
152+
153+
// You can use safeRemove method to delete the folder without any exception if it does no exists
154+
155+
await Folder.safeRemove(existentFile.path)
156+
```
157+
158+
---
159+
160+
### Is
161+
162+
> Use Is to validate if value is from some type or is empty, is uuid, is cpf, is cep, etc...
163+
164+
```ts
165+
import { Is } from '@secjs/utils'
166+
167+
// Is class is a validator. It validates if the value matches the name of the function and returns a boolean
168+
169+
Is.Empty('') // true
170+
Is.Empty([]) // true
171+
Is.Empty([1]) // false
172+
Is.Empty({}) // true
173+
Is.Empty({ hello: 'world' }) // false
174+
Is.Empty(' ') // true
175+
Is.Empty('hello') // false
176+
177+
Is.Uuid('not-valid-uuid') // false
178+
Is.Cep('not-valid-cep') // false
179+
Is.Cpf('not-valid-cpf') // false
180+
Is.Cnpj('not-valid-cnpj') // false
181+
182+
Is.String('value') // true
183+
Is.Undefined('value') // false
184+
Is.Null('value') // false
185+
Is.Boolean('value') // false
186+
Is.Buffer('value') // false
187+
Is.Number('value') // false
188+
Is.Object('value') // false
189+
Is.Date('value') // false
190+
Is.Array('value') // false
191+
Is.Regexp('value') // false
192+
Is.Error('value') // false
193+
Is.Function('value') // false
194+
Is.Class('value') // false
195+
Is.Integer('value') // false
196+
Is.Float('value') // false
197+
```
198+
199+
---
200+
201+
### String
202+
203+
> Use String to generate random strings, normalizations and case changes
204+
205+
```ts
206+
import { String } from '@secjs/utils'
207+
208+
// With String you can change the case of strings
209+
210+
const string = 'Hello world'
211+
const capitalize = true
212+
213+
String.toCamelCase(string) // 'helloWorld'
214+
String.toPascalCase(string) // 'HelloWorld'
215+
String.toSnakeCase(string) // 'hello_world'
216+
String.toDotCase(string) // 'hello.world'
217+
String.toSentenceCase(string) // 'Hello world'
218+
String.toNoCase(string) // 'hello world'
219+
String.toDashCase(string) // 'hello-world'
220+
String.toDashCase(string, capitalize) // 'Hello-World'
221+
222+
// You can generate random strings by size and random hexadecimal colors
223+
224+
String.generateRandom(10) // 'GpXuZScThi'
225+
String.generateRandomColor() // '#d5063b'
226+
227+
// You can put a string in plural or in singular and in ordinal number
228+
229+
String.pluralize(string) // 'Hello worlds'
230+
String.singularize(String.pluralize(string)) // 'Hello world'
231+
String.ordinalize('1') // '1st'
232+
String.ordinalize('2') // '2nd'
233+
String.ordinalize('3') // '3rd'
234+
String.ordinalize('10') // '10th'
235+
236+
// And you can also normalize base64 string
237+
238+
String.normalizeBase64('+++///===') // '---___'
148239
```
149240

150241
---
@@ -376,12 +467,29 @@ console.log(isUuid) // true
376467
```ts
377468
import { Parser } from '@secjs/utils'
378469

470+
// Convert a string to array using a separator
471+
379472
const string1 = '1,2,3'
380-
const parsed1 = Parser.stringToArray(string1)
473+
const separator = ','
474+
const parsed1 = Parser.stringToArray(string1, separator)
381475

382476
console.log(parsed1) // ['1', '2', '3']
383477
```
384478

479+
```ts
480+
// Convert an array to string using separators
481+
482+
Parser.arrayToString(['1', '2', '3', '4']) // '1, 2, 3 and 4'
483+
Parser.arrayToString(['1', '2', '3', '4'], // '1|2|3-4'
484+
{ separator: '|', lastSeparator: '-' }
485+
)
486+
487+
// Pair separator is only for and array of two indexes
488+
Parser.arrayToString(['1', '2'], { // '1_2'
489+
pairSeparator: '_',
490+
})
491+
```
492+
385493
```ts
386494
const string2 = 'aaaasadzczaaa21313'
387495
const parsed2 = Parser.stringToNumber(string2)
@@ -406,16 +514,51 @@ console.log(parsed4) // { joao: 'joao', email: 'lenonsec7@gmail.com' }
406514
```
407515

408516
```ts
409-
const bytes = 1024*1024*1024 // 1GB
410-
const decimals = 4
517+
const message = 'Link: https://google.com'
518+
519+
// Convert url to and HTML href
411520

412-
Parser.bytesToSize(bytes, decimals) // Example: 1.0932 GB
521+
console.log(Parser.linkToHref(message)) // Link: <a href="https://google.com">https://google.com</a>
413522
```
414523

415524
```ts
416-
const message = 'Link: https://google.com'
525+
// Convert number size to bytes
526+
527+
Parser.sizeToByte(1024) // '1KB'
528+
Parser.sizeToByte(1048576) // '1MB'
529+
Parser.sizeToByte(1073741824) // '1GB'
530+
Parser.sizeToByte(1099511627776) // '1TB'
531+
Parser.sizeToByte(1125899906842624) // '1PB'
532+
533+
// Convert bytes to number size
534+
535+
Parser.byteToSize('1KB') // 1024
536+
Parser.byteToSize('1MB') // 1048576
537+
Parser.byteToSize('1GB') // 1073741824
538+
Parser.byteToSize('1TB') // 1099511627776
539+
Parser.byteToSize('1PB') // 1125899906842624
540+
```
541+
542+
```ts
543+
// Convert time string to ms
544+
545+
Parser.timeToMs('2 days') // 172800000
546+
Parser.timeToMs('1d') // 86400000
547+
Parser.timeToMs('10h') // 36000000
548+
Parser.timeToMs('-10h') // -36000000
549+
Parser.timeToMs('1 year') // 31557600000
550+
Parser.timeToMs('-1 year') // -31557600000
551+
552+
// Convert ms to time string
417553

418-
console.log(urlify(message)) // Link: <a href="https://google.com">https://google.com</a>
554+
const long = true
555+
556+
Parser.msToTime(172800000, long) // '2 days'
557+
Parser.msToTime(86400000) // 1d
558+
Parser.msToTime(36000000) // 10h
559+
Parser.msToTime(-36000000) // -10h
560+
Parser.msToTime(31557600000, long) // 1 year
561+
Parser.msToTime(-31557600000, long) // -1 year
419562
```
420563

421564
---
@@ -494,18 +637,6 @@ import { download } from '@secjs/utils'
494637

495638
---
496639

497-
### randomColor
498-
499-
> Use randomColor to generate a random Hexadecimal color
500-
501-
```js
502-
import { randomColor } from '@secjs/utils'
503-
504-
console.log(randomColor()) // #7059c1
505-
```
506-
507-
---
508-
509640
### scheduler
510641

511642
> Use scheduler to execute some function based on MS
@@ -566,20 +697,6 @@ console.log(paginate(array, total, pagination))
566697

567698
---
568699

569-
### random
570-
571-
> Use random to generate random strings by the length you want using crypto
572-
573-
```js
574-
import { random } from '@secjs/utils'
575-
576-
const randomStringWith10Chars = await random(10)
577-
578-
console.log(randomStringWith10Chars) // qwiortlkps
579-
```
580-
581-
---
582-
583700
### sleep
584701

585702
> Use sleep to let you code sleep for sometime
@@ -619,44 +736,6 @@ console.log(distance) // The distance in Kilometers (KM)
619736

620737
---
621738

622-
### isCpf
623-
624-
> Validate if is a valid CPF Document or not.
625-
626-
```js
627-
import { isCpf } from '@secjs/utils'
628-
629-
// CPF (911.881.600-28) Generated using https://4devs.com.br
630-
631-
console.log(isCpf(91188160028)) // true
632-
console.log(isCpf("91188160028")) // true
633-
console.log(isCpf("911.881.600-28")) // true
634-
635-
console.log(isCpf("911.881.600-29")) // false
636-
console.log(isCpf("000.000.000-00")) // false
637-
```
638-
639-
---
640-
641-
### isCnpj
642-
643-
> Validate if is a valid CNPJ Document or not.
644-
645-
```js
646-
import { isCnpj } from '@secjs/utils'
647-
648-
// CNPJ (77.111.157/0001-19) Generated using https://4devs.com.br
649-
650-
console.log(isCnpj(77111157000119)) // true
651-
console.log(isCnpj("77111157000119")) // true
652-
console.log(isCnpj("77.111.157/0001-19")) // true
653-
654-
console.log(isCnpj("77.111.157/0001-20")) // false
655-
console.log(isCnpj("00.000.000/0000-00")) // false
656-
```
657-
658-
---
659-
660739
## License
661740

662741
Made with 🖤 by [jlenon7](https://github.com/jlenon7) :wave:

index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './src/Classes/Is'
12
export * from './src/Classes/Json'
23
export * from './src/Classes/Path'
34
export * from './src/Classes/File'
@@ -6,18 +7,14 @@ export * from './src/Classes/Clean'
67
export * from './src/Classes/Route'
78
export * from './src/Classes/Folder'
89
export * from './src/Classes/Parser'
10+
export * from './src/Classes/String'
911
export * from './src/Classes/Numbers'
1012
export * from './src/Classes/Blacklist'
1113

12-
export * from './src/Functions/unset'
1314
export * from './src/Functions/sleep'
14-
export * from './src/Functions/isCpf'
15-
export * from './src/Functions/isCnpj'
16-
export * from './src/Functions/random'
1715
export * from './src/Functions/download'
1816
export * from './src/Functions/kmRadius'
1917
export * from './src/Functions/paginate'
2018
export * from './src/Functions/scheduler'
2119
export * from './src/Functions/getBranch'
2220
export * from './src/Functions/getCommitId'
23-
export * from './src/Functions/randomColor'

0 commit comments

Comments
 (0)