Skip to content

Commit c616d2f

Browse files
committed
feat(pkg): add new methods
1 parent 66e250b commit c616d2f

File tree

14 files changed

+349
-22
lines changed

14 files changed

+349
-22
lines changed

package-lock.json

Lines changed: 58 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"chalk": "^5.4.1",
8181
"change-case": "^4.1.2",
8282
"collect.js": "^4.36.1",
83+
"crc": "^4.3.2",
8384
"csv-parser": "^3.1.0",
8485
"deasync": "^0.1.30",
8586
"execa": "^8.0.1",
@@ -94,6 +95,7 @@
9495
"mime-types": "^2.1.35",
9596
"minimatch": "^5.1.6",
9697
"ms": "^2.1.3",
98+
"otp-generator": "^4.0.1",
9799
"parent-module": "^3.1.0",
98100
"pluralize": "^8.0.0",
99101
"prepend-file": "^2.0.1",
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @athenna/common
3+
*
4+
* (c) João Lenon <lenon@athenna.io>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import { Exception } from '#src/helpers/Exception'
11+
12+
export class NotFoundAthennaConfig extends Exception {
13+
public constructor() {
14+
super({
15+
code: 'E_NOT_FOUND_ATHENNA_CONFIG',
16+
message: 'The @athenna/config package is not installed',
17+
help: 'String.hash() requires @athenna/config to be installed to search for your app.key. You bypass installing @athenna/config if you define the key when calling the method: String.hash("value", { key: "your-key" })'
18+
})
19+
}
20+
}

src/globals/Array.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,23 @@ export class AthennaArray<T> {
5151
}
5252

5353
/**
54-
* Remove all duplicated values from the array.
54+
* Create a new array only with the unique values.
5555
*
5656
* @example
5757
* ```ts
58-
* [1, 2, 2, 3].removeDuplicated() // [1, 2, 3]
58+
* [1, 2, 2, 3].unique() // [1, 2, 3]
5959
* ```
6060
*/
61-
public removeDuplicated() {
61+
public unique(key?: keyof T) {
62+
if (key) {
63+
const seen = new Set()
64+
65+
return this.items.filter(item => {
66+
const k = item[key]
67+
return seen.has(k) ? false : seen.add(k)
68+
})
69+
}
70+
6271
return [...new Set(this.items)]
6372
}
6473

src/helpers/Collection.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ export class Collection<T = any> extends CollectJS<T> {
2525
/**
2626
* Remove all duplicated values from the array.
2727
*
28+
* @deprecated Use unique() method instead. This method will
29+
* me removed on the next major release.
2830
* @example
2931
* ```ts
3032
* new Collection([1, 2, 2, 3]).removeDuplicated().all() // [1, 2, 3]
3133
* ```
3234
*/
3335
public removeDuplicated(): T[] {
34-
return this.all().athenna.removeDuplicated()
36+
return this.all().athenna.unique()
3537
}
3638

3739
/**

src/helpers/Is.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ import { isIP } from 'node:net'
1313
import { File } from '#src/helpers/File'
1414
import { Uuid } from '#src/helpers/Uuid'
1515
import { Ulid } from '#src/helpers/Ulid'
16+
import { String } from '#src/helpers/String'
1617
import { Exception } from '#src/helpers/Exception'
1718
import { isCep, isCnpj, isCpf } from 'validator-brazil'
1819

1920
export class Is {
21+
private static styleFileRegex =
22+
/\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\?)/
23+
2024
/**
2125
* Return the kindOf.
2226
*/
@@ -185,6 +189,19 @@ export class Is {
185189
return isCnpj(cnpj)
186190
}
187191

192+
/**
193+
* Verify if string is a valid CSS path.
194+
*
195+
* @example
196+
* ```ts
197+
* if (Is.CssPath(Path.pwd('app.css'))) {
198+
* }
199+
* ```
200+
*/
201+
public static CssPath(path: string) {
202+
return path.match(Is.styleFileRegex) !== null
203+
}
204+
188205
/**
189206
* Verify if is a valid Async function.
190207
*/
@@ -325,4 +342,25 @@ export class Is {
325342

326343
return !results.includes(false)
327344
}
345+
346+
/**
347+
* Validate if a given hash matches a given value.
348+
*
349+
* @example
350+
* ```ts
351+
* const value = 'value'
352+
* const hash = 'hash-of-value'
353+
*
354+
* if (Is.ValidHash(hash, value)) {}
355+
*
356+
* if (Is.ValidHash(hash, value, { key: 'my-secret', prefix: 'token_' })) {}
357+
* ```
358+
*/
359+
public static ValidHash(
360+
hash: string,
361+
value: string,
362+
options: { key?: string; prefix?: string } = {}
363+
): boolean {
364+
return hash === String.hash(value, options)
365+
}
328366
}

src/helpers/Parser.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
csv2json,
2727
type Csv2JsonOptions
2828
} from 'json-2-csv'
29+
import type { HTMLJson } from '#src/types/json/HTMLJson'
2930

3031
export class Parser {
3132
/**
@@ -130,6 +131,49 @@ export class Parser {
130131
return object
131132
}
132133

134+
/**
135+
* Parse an object to HTML element.
136+
*
137+
* @example
138+
* ```ts
139+
* const htmlElement = Parser.jsonToHTML({
140+
* tag: 'script',
141+
* attributes: {
142+
* type: 'module',
143+
* src: 'app.css',
144+
* },
145+
* children: []
146+
* })
147+
* // `<script type="module" src="app.css"></script>`
148+
* ```
149+
*/
150+
public static jsonToHTML(element: HTMLJson): string {
151+
const attributes = `${Object.keys(element.attributes)
152+
.map(key => {
153+
const value = attributes[key]
154+
155+
if (value === true) {
156+
return key
157+
}
158+
159+
if (!value) {
160+
return null
161+
}
162+
163+
return `${key}="${value}"`
164+
})
165+
.filter(attr => attr !== null)
166+
.join(' ')}`
167+
168+
if (element.tag === 'link') {
169+
return `<${element.tag} ${attributes}/>`
170+
}
171+
172+
return `<${element.tag} ${attributes}>${element.children.join('\n')}</${
173+
element.tag
174+
}>`
175+
}
176+
133177
/**
134178
* Parse object to yaml string.
135179
*/

0 commit comments

Comments
 (0)