11// Import Internal Dependencies
2- import * as Literal from "./literal.js" ;
2+ import { toRaw , toValue , type ESTreeLiteral } from "./literal.js" ;
33import * as Utils from "./utils.js" ;
44
5+ // CONSTANTS
56const kUnsafeHexValues = new Set ( [
67 "require" ,
78 "length"
89] . map ( ( value ) => Buffer . from ( value ) . toString ( "hex" ) ) ) ;
9-
10- // CONSTANTS
1110const kSafeHexValues = new Set ( [
1211 "0123456789" ,
1312 "123456789" ,
@@ -24,30 +23,36 @@ export const CONSTANTS = Object.freeze({
2423
2524/**
2625 * @description detect if the given string is an Hexadecimal value
27- * @param {SecLiteral.Literal | string } anyValue
28- * @returns {boolean }
2926 */
30- export function isHex ( anyValue ) {
31- const value = Literal . toValue ( anyValue ) ;
27+ export function isHex (
28+ anyValue : ESTreeLiteral | string
29+ ) : boolean {
30+ const value = toValue ( anyValue ) ;
3231
3332 return typeof value === "string" && / ^ [ 0 - 9 A - F a - f ] { 4 , } $ / g. test ( value ) ;
3433}
3534
3635/**
3736 * @description detect if the given string is a safe Hexadecimal value
38- * @param {SecLiteral.Literal | string } anyValue
39- * @returns {boolean }
4037 */
41- export function isSafe ( anyValue ) {
42- const rawValue = Literal . toRaw ( anyValue ) ;
43- if ( kUnsafeHexValues . has ( rawValue ) ) {
38+ export function isSafe (
39+ anyValue : ESTreeLiteral | string
40+ ) : boolean {
41+ const rawValue = toRaw ( anyValue ) ;
42+ if ( typeof rawValue === "undefined" || kUnsafeHexValues . has ( rawValue ) ) {
4443 return false ;
4544 }
4645
4746 const charCount = Utils . stringCharDiversity ( rawValue ) ;
48- if ( / ^ ( [ 0 - 9 ] + | [ a - z ] + | [ A - Z ] + ) $ / g. test ( rawValue ) || rawValue . length <= 5 || charCount <= 2 ) {
47+ if (
48+ / ^ ( [ 0 - 9 ] + | [ a - z ] + | [ A - Z ] + ) $ / g. test ( rawValue )
49+ || rawValue . length <= 5
50+ || charCount <= 2
51+ ) {
4952 return true ;
5053 }
5154
52- return [ ...kSafeHexValues ] . some ( ( value ) => rawValue . toLowerCase ( ) . startsWith ( value ) ) ;
55+ return [ ...kSafeHexValues ] . some (
56+ ( value ) => rawValue . toLowerCase ( ) . startsWith ( value )
57+ ) ;
5358}
0 commit comments