@@ -12,7 +12,16 @@ import bytes from 'bytes'
1212import yaml from 'js-yaml'
1313import csvParser from 'csv-parser'
1414
15+ import {
16+ json2csv ,
17+ csv2json ,
18+ type Json2CsvOptions ,
19+ type Csv2JsonOptions
20+ } from 'json-2-csv'
21+ import type { HTMLJson } from '#src/types/json/HTMLJson'
22+
1523import { Is } from '#src/helpers/Is'
24+ import { Clean } from '#src/helpers/Clean'
1625import { String } from '#src/helpers/String'
1726import { Options } from '#src/helpers/Options'
1827import { Macroable } from '#src/helpers/Macroable'
@@ -21,14 +30,6 @@ import type { ObjectBuilderOptions } from '#src/types'
2130import { getReasonPhrase , getStatusCode } from 'http-status-codes'
2231import { InvalidNumberException } from '#src/exceptions/InvalidNumberException'
2332
24- import {
25- json2csv ,
26- csv2json ,
27- type Json2CsvOptions ,
28- type Csv2JsonOptions
29- } from 'json-2-csv'
30- import type { HTMLJson } from '#src/types/json/HTMLJson'
31-
3233export class Parser extends Macroable {
3334 /**
3435 * Parse using Node.js streams, useful for
@@ -216,6 +217,68 @@ export class Parser extends Macroable {
216217 return bytes . parse ( byte )
217218 }
218219
220+ /**
221+ * Parses an object to a base64 string.
222+ */
223+ public static objectToBase64 (
224+ object : Record < string , any > ,
225+ options ?: { nullOnEmpty ?: boolean ; removeEmpty ?: boolean }
226+ ) {
227+ options = Options . create ( options , {
228+ nullOnEmpty : false ,
229+ removeEmpty : false
230+ } )
231+
232+ if ( options . removeEmpty ) {
233+ object = Clean . cleanObject ( object , { removeEmpty : true , recursive : true } )
234+ }
235+
236+ if ( options . nullOnEmpty && Is . Empty ( object ) ) {
237+ return null
238+ }
239+
240+ return Buffer . from ( JSON . stringify ( object ) ) . toString ( 'base64' )
241+ }
242+
243+ /**
244+ * Parses a base64 string to an object.
245+ */
246+ public static base64ToObject < T = Record < string , any > > ( base64 : string ) : T {
247+ return JSON . parse ( Buffer . from ( base64 , 'base64' ) . toString ( 'utf-8' ) )
248+ }
249+
250+ /**
251+ * Parses an object to a base64url string.
252+ */
253+ public static objectToBase64Url (
254+ object : Record < string , any > ,
255+ options ?: { nullOnEmpty ?: boolean ; removeEmpty ?: boolean }
256+ ) {
257+ options = Options . create ( options , {
258+ nullOnEmpty : false ,
259+ removeEmpty : false
260+ } )
261+
262+ if ( options . removeEmpty ) {
263+ object = Clean . cleanObject ( object , { removeEmpty : true , recursive : true } )
264+ }
265+
266+ if ( options . nullOnEmpty && Is . Empty ( object ) ) {
267+ return null
268+ }
269+
270+ return Buffer . from ( JSON . stringify ( object ) ) . toString ( 'base64url' )
271+ }
272+
273+ /**
274+ * Parses a base64url string to an object.
275+ */
276+ public static base64UrlToObject < T = Record < string , any > > (
277+ base64Url : string
278+ ) : T {
279+ return JSON . parse ( Buffer . from ( base64Url , 'base64url' ) . toString ( 'utf-8' ) )
280+ }
281+
219282 /**
220283 * Parses a string to MS format.
221284 */
@@ -230,6 +293,20 @@ export class Parser extends Macroable {
230293 return ms ( value , { long } )
231294 }
232295
296+ /**
297+ * Parses a time string to seconds format.
298+ */
299+ public static timeToSeconds ( value : string ) : number {
300+ return ms ( value ) / 1000
301+ }
302+
303+ /**
304+ * Parses a seconds number to time format.
305+ */
306+ public static secondsToTime ( value : number , long = false ) : string {
307+ return ms ( value * 1000 , { long } )
308+ }
309+
233310 /**
234311 * Parses a json to a csv string.
235312 */
0 commit comments