Skip to content

Commit 29dae3a

Browse files
authored
Merge pull request #27 from SecJS/refactor/len-move-fns-add-statics
refactor: Remove fns, update docs and add statics
2 parents c5c6999 + e0c955a commit 29dae3a

30 files changed

+337
-456
lines changed

README.md

Lines changed: 105 additions & 167 deletions
Large diffs are not rendered by default.

index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,15 @@ export * from './src/Classes/Parser'
99
export * from './src/Classes/Numbers'
1010
export * from './src/Classes/Blacklist'
1111

12-
export * from './src/Functions/sort'
1312
export * from './src/Functions/unset'
1413
export * from './src/Functions/sleep'
1514
export * from './src/Functions/isCpf'
1615
export * from './src/Functions/isCnpj'
1716
export * from './src/Functions/random'
18-
export * from './src/Functions/urlify'
1917
export * from './src/Functions/download'
20-
export * from './src/Functions/fillable'
2118
export * from './src/Functions/kmRadius'
2219
export * from './src/Functions/paginate'
2320
export * from './src/Functions/scheduler'
2421
export * from './src/Functions/getBranch'
2522
export * from './src/Functions/getCommitId'
2623
export * from './src/Functions/randomColor'
27-
export * from './src/Functions/observeChanges'
28-
export * from './src/Functions/removeDuplicated'
29-
export * from './src/Functions/isArrayOfObjects'

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/utils",
3-
"version": "1.4.1",
3+
"version": "1.4.2",
44
"description": "",
55
"license": "MIT",
66
"author": "João Lenon",

src/Classes/Clean.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export class Clean {
2-
isEmpty(data: any | any[] | string): boolean {
2+
static isEmpty(data: any | any[] | string): boolean {
33
if (!data) {
44
return true
55
}
@@ -26,7 +26,7 @@ export class Clean {
2626
* @param removeEmpty Remove all empty objects/arrays from array
2727
* @return The array filtered without any falsy value
2828
*/
29-
cleanArray(
29+
static cleanArray(
3030
array: any[],
3131
removeEmpty = false,
3232
cleanInsideObjects = false,
@@ -62,7 +62,7 @@ export class Clean {
6262
* @param removeEmpty Remove all empty objects/arrays from object
6363
* @return The object filtered without any falsy value
6464
*/
65-
cleanObject(
65+
static cleanObject(
6666
object: any,
6767
removeEmpty = false,
6868
cleanInsideArrays = false,

src/Classes/File.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import {
2222
} from 'fs'
2323

2424
import { Path } from './Path'
25+
import { Parser } from './Parser'
2526
import { lookup } from 'mime-types'
2627
import { randomBytes } from 'crypto'
2728
import { parse, isAbsolute } from 'path'
28-
import { formatBytes } from '../Functions/formatBytes'
2929
import { InternalServerException } from '@secjs/exceptions'
3030

3131
export interface FileJsonContract {
@@ -161,7 +161,7 @@ export class File {
161161
this._createdAt = fileStat.birthtime
162162
this._accessedAt = fileStat.atime
163163
this._modifiedAt = fileStat.mtime
164-
this._fileSize = formatBytes(fileStat.size, 4)
164+
this._fileSize = Parser.bytesToSize(fileStat.size, 4)
165165

166166
if (options.withContent) {
167167
// 200mb
@@ -195,7 +195,7 @@ export class File {
195195
this._accessedAt = fileStat.atime
196196
this._modifiedAt = fileStat.mtime
197197
this._createdAt = fileStat.birthtime
198-
this._fileSize = formatBytes(fileStat.size, 4)
198+
this._fileSize = Parser.bytesToSize(fileStat.size, 4)
199199

200200
if (!options.withContent) return this
201201

src/Classes/Folder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import {
2121

2222
import { File } from './File'
2323
import { Path } from './Path'
24+
import { Parser } from './Parser'
2425
import { randomBytes } from 'crypto'
2526
import { isAbsolute, join, parse, resolve } from 'path'
26-
import { formatBytes } from '../Functions/formatBytes'
2727
import { InternalServerException } from '@secjs/exceptions'
2828

2929
export interface FolderJsonContract {
@@ -132,7 +132,7 @@ export class Folder {
132132
this._createdAt = fileStat.birthtime
133133
this._accessedAt = fileStat.atime
134134
this._modifiedAt = fileStat.mtime
135-
this._folderSize = formatBytes(fileStat.size, 4)
135+
this._folderSize = Parser.bytesToSize(fileStat.size, 4)
136136

137137
if (options.withSub) {
138138
this.loadSubSync(
@@ -170,7 +170,7 @@ export class Folder {
170170
this._createdAt = folderStat.birthtime
171171
this._accessedAt = folderStat.atime
172172
this._modifiedAt = folderStat.mtime
173-
this._folderSize = formatBytes(folderStat.size, 4)
173+
this._folderSize = Parser.bytesToSize(folderStat.size, 4)
174174

175175
if (options.withSub) {
176176
await this.loadSub(

src/Classes/Json.ts

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export class Json {
55
* @param array The array to be validated
66
* @return true or false
77
*/
8-
isArrayOfObjects(array: any[]): boolean {
8+
static isArrayOfObjects(array: any[]): boolean {
99
if (!array.length) return false
1010

1111
const results = array.map(object => typeof object === 'object')
@@ -19,7 +19,7 @@ export class Json {
1919
* @param object The object to be copied
2020
* @return A copy from object without any reference
2121
*/
22-
copy<T>(object: T): T {
22+
static copy<T>(object: T): T {
2323
const copy: any = {}
2424

2525
for (const i in object) {
@@ -36,7 +36,7 @@ export class Json {
3636
* @param text A valid string with one or more JSON's inside.
3737
* @returns An array of JSON's found in the string.
3838
*/
39-
getJson(text: string): string[] {
39+
static getJson(text: string): string[] {
4040
let match: RegExpExecArray
4141
const matchs = []
4242

@@ -55,7 +55,7 @@ export class Json {
5555
* @param reviver A function that transforms the results. This function is called for each member of the object.
5656
* If a member contains nested objects, the nested objects are transformed before the parent object is.
5757
*/
58-
parse(
58+
static parse(
5959
text: string,
6060
reviver?: (this: any, key: string, value: any) => any,
6161
): any {
@@ -65,4 +65,63 @@ export class Json {
6565
return null
6666
}
6767
}
68+
69+
/**
70+
* Observe changes inside objects
71+
*
72+
* @param object - The object to be observed
73+
* @param func - The function that will execute all the time the object has been modified
74+
* @param args - The arguments of the function
75+
* @return proxy - A Proxy instance
76+
*/
77+
static observeChanges(object: any, func: any, ...args: any[]): any {
78+
return new Proxy(object, {
79+
set: (target, key, value) => {
80+
func(value, ...args)
81+
82+
target[key] = value
83+
84+
return true
85+
},
86+
})
87+
}
88+
89+
/**
90+
* Remove all keys from data that is not inside array keys
91+
*
92+
* @param data The data with keys to remove
93+
* @param keys Array of keys that needs to stay in data
94+
* @return data only with keys
95+
*/
96+
static fillable(data: any, keys: string[]): any {
97+
return keys.reduce((previous: any, key: string) => {
98+
if (data[key]) {
99+
previous[key] = data[key]
100+
}
101+
102+
return previous
103+
}, {})
104+
}
105+
106+
/**
107+
* Remove all duplicated values from the array
108+
*
109+
* @param array - The array to be verified
110+
* @return array - The array without duplicated values
111+
*/
112+
static removeDuplicated(array: any[]): any[] {
113+
return [...new Set(array)]
114+
}
115+
116+
/**
117+
* Sort an index value from the array
118+
*
119+
* @param array - The array with n index
120+
* @return any - A random value from the array
121+
*/
122+
static sort(array: any): any {
123+
const index = Math.random() * array.length
124+
125+
return array[Math.floor(index)]
126+
}
68127
}

src/Classes/Numbers.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class Numbers {
77
* @param numbers The array of numbers
88
* @return The higher number from the array
99
*/
10-
getHigher(numbers: number[]): number {
10+
static getHigher(numbers: number[]): number {
1111
// eslint-disable-next-line prefer-spread
1212
return Math.max.apply(Math, numbers)
1313
}
@@ -18,7 +18,7 @@ export class Numbers {
1818
* @param numbers The array of numbers
1919
* @return The lower number from the array
2020
*/
21-
getLower(numbers: number[]): number {
21+
static getLower(numbers: number[]): number {
2222
// eslint-disable-next-line prefer-spread
2323
return Math.min.apply(Math, numbers)
2424
}
@@ -29,8 +29,8 @@ export class Numbers {
2929
* @param string The string with numbers inside
3030
* @return One number only
3131
*/
32-
extractNumber(string: string): number {
33-
return new Parser().stringToNumber(string.replace(/\D/g, ''))
32+
static extractNumber(string: string): number {
33+
return Parser.stringToNumber(string.replace(/\D/g, ''))
3434
}
3535

3636
/**
@@ -39,9 +39,9 @@ export class Numbers {
3939
* @param string The string with numbers inside
4040
* @return An array of numbers
4141
*/
42-
extractNumbers(string: string): number[] {
42+
static extractNumbers(string: string): number[] {
4343
return string.match(/[0-9]+/g).map(numberString => {
44-
return new Parser().stringToNumber(numberString)
44+
return Parser.stringToNumber(numberString)
4545
})
4646
}
4747

@@ -51,7 +51,7 @@ export class Numbers {
5151
* @param args The array of numbers from all function arguments
5252
* @return The average of all numbers
5353
*/
54-
argsAverage(...args: number[]): number {
54+
static argsAverage(...args: number[]): number {
5555
return args.reduce((acc: any, curr: any) => acc + curr, 0) / args.length
5656
}
5757

@@ -61,7 +61,7 @@ export class Numbers {
6161
* @param array The array of numbers
6262
* @return The average of all numbers
6363
*/
64-
arrayAverage(array: number[]): number {
64+
static arrayAverage(array: number[]): number {
6565
return array.reduce((acc, curr) => acc + curr, 0) / array.length
6666
}
6767
}

src/Classes/Parser.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class Parser {
88
* @param separator The separator to use
99
* @return The array of string
1010
*/
11-
stringToArray(string: string, separator: string): string[] {
11+
static stringToArray(string: string, separator: string): string[] {
1212
return string.split(separator).map(index => index.trim())
1313
}
1414

@@ -19,7 +19,7 @@ export class Parser {
1919
* @param isCoordinate If string is a coordinate
2020
* @return The string parsed to int or float
2121
*/
22-
stringToNumber(string: string, isCoordinate = false): number {
22+
static stringToNumber(string: string, isCoordinate = false): number {
2323
if (!string.replace(/\D/g, '')) {
2424
throw new InternalServerException(
2525
'Your string is invalid, it should have at least one number.',
@@ -41,7 +41,7 @@ export class Parser {
4141
* @param object The object to parse
4242
* @return The object parsed to form data
4343
*/
44-
jsonToFormData(object: any): string {
44+
static jsonToFormData(object: any): string {
4545
return Object.keys(object)
4646
.reduce((previous, current) => {
4747
return previous + `&${current}=${encodeURIComponent(object[current])}`
@@ -55,7 +55,7 @@ export class Parser {
5555
* @param formData The form data to parse
5656
* @return The form data parsed to object
5757
*/
58-
formDataToJson(formData: string): any {
58+
static formDataToJson(formData: string): any {
5959
const object = {}
6060

6161
if (formData.startsWith('?')) formData = formData.replace('?', '')
@@ -68,4 +68,35 @@ export class Parser {
6868

6969
return object
7070
}
71+
72+
/**
73+
* bytesToSize creates a string based on the bytes size
74+
*
75+
* @param bytes - The number of bytes
76+
* @param decimals - The number of decimals to be showed
77+
* @return formattedSize - Return the formatted value based on the size (100 MB, 1 GB, etc)
78+
*/
79+
static bytesToSize(bytes: number, decimals = 2) {
80+
if (bytes === 0) return '0 Bytes'
81+
82+
const k = 1024
83+
const dm = decimals < 0 ? 0 : decimals
84+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
85+
86+
const i = Math.floor(Math.log(bytes) / Math.log(k))
87+
88+
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
89+
}
90+
91+
/**
92+
* linkToHref parses all links inside the string to HTML link with <a href= .../>
93+
*
94+
* @param string - The string with links inside
95+
* @return formattedString - Return the formatted string
96+
*/
97+
static linkToHref(string: any): string {
98+
const regex = /(https?:\/\/[^\s]+)/g
99+
100+
return string.replace(regex, '<a href="$1">$1</a>')
101+
}
71102
}

0 commit comments

Comments
 (0)