-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
parse bigint or other types when save to cache
- Loading branch information
Showing
6 changed files
with
130 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './parser' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { ArgParser } from './parser' | ||
|
||
describe('ArgParser', () => { | ||
it('should parse and serialize basic types', () => { | ||
const parser = new ArgParser() | ||
const args = [42, 'hello', true, BigInt(12345678901234567890n)] | ||
const parsedArgs = parser.parse(args) | ||
const serializedArgs = parser.serialize(parsedArgs) | ||
expect(serializedArgs).toEqual(args) | ||
}) | ||
|
||
it('should parse and serialize nested structures', () => { | ||
const parser = new ArgParser() | ||
const args = [42, 'hello', true, { a: 1, b: 'world', c: [1, 2, { d: 'nested' }] }] | ||
const parsedArgs = parser.parse(args) | ||
const serializedArgs = parser.serialize(parsedArgs) | ||
expect(serializedArgs).toEqual(args) | ||
}) | ||
|
||
it('should handle empty arrays and objects', () => { | ||
const parser = new ArgParser() | ||
const args = [[], {}] | ||
const parsedArgs = parser.parse(args) | ||
const serializedArgs = parser.serialize(parsedArgs) | ||
expect(serializedArgs).toEqual(args) | ||
}) | ||
|
||
it('should handle complex nested structures with bigint', () => { | ||
const parser = new ArgParser() | ||
const args = [ | ||
42, | ||
'hello', | ||
true, | ||
{ | ||
a: 1, | ||
b: 'world', | ||
c: [1, 2, { d: 'nested', e: BigInt(9876543210987654321n) }], | ||
}, | ||
BigInt(12345678901234567890n), | ||
] | ||
const parsedArgs = parser.parse(args) | ||
const serializedArgs = parser.serialize(parsedArgs) | ||
expect(serializedArgs).toEqual(args) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* eslint-disable no-prototype-builtins */ | ||
/* eslint-disable no-case-declarations */ | ||
export interface ParsedArg { | ||
value: any | ||
type: string | ||
} | ||
|
||
export class ArgParser { | ||
parse(args: any[]): ParsedArg[] { | ||
return args.map(arg => this._parse(arg)) | ||
} | ||
|
||
private _parse(arg: any): ParsedArg { | ||
const type = typeof arg | ||
if (Array.isArray(arg)) { | ||
return { | ||
value: arg.map(item => this._parse(item)), | ||
type: 'array', | ||
} | ||
} | ||
else if (type === 'object' && arg !== null) { | ||
const parsedObject: { [key: string]: ParsedArg } = {} | ||
for (const key in arg) { | ||
if (arg.hasOwnProperty(key)) | ||
parsedObject[key] = this._parse(arg[key]) | ||
} | ||
return { | ||
value: parsedObject, | ||
type: 'object', | ||
} | ||
} | ||
else if (type === 'bigint') { | ||
return { | ||
value: arg.toString(), | ||
type: 'bigint', | ||
} | ||
} | ||
else { | ||
return { | ||
value: arg, | ||
type, | ||
} | ||
} | ||
} | ||
|
||
serialize(parsedArgs: ParsedArg[]): any[] { | ||
return parsedArgs.map(parsedArg => this._serialize(parsedArg)) | ||
} | ||
|
||
private _serialize(parsedArg: ParsedArg): any { | ||
switch (parsedArg.type) { | ||
case 'string': | ||
return String(parsedArg.value) | ||
case 'number': | ||
return Number(parsedArg.value) | ||
case 'boolean': | ||
return Boolean(parsedArg.value) | ||
case 'bigint': | ||
return BigInt(parsedArg.value) | ||
case 'array': | ||
return (parsedArg.value as ParsedArg[]).map(item => this._serialize(item)) | ||
case 'object': | ||
const serializedObject: { [key: string]: any } = {} | ||
const value = parsedArg.value as { [key: string]: ParsedArg } | ||
for (const key in value) { | ||
if (value.hasOwnProperty(key)) | ||
serializedObject[key] = this._serialize(value[key]) | ||
} | ||
return serializedObject | ||
default: | ||
return parsedArg.value | ||
} | ||
} | ||
} | ||
|
||
export const argParser = new ArgParser() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export * from './common' | ||
export * from './store' | ||
export * from './w3' | ||
export * from './args' | ||
|
||
export type * from './types' | ||
export * from '@murongg/utils' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters