@@ -11,7 +11,7 @@ const NAME_RE = /^[\da-z-]{1,32}$/,
1111 String . raw `$` ) ;
1212
1313// public
14- export function encode ( { id, version, params, salt, hash, defaultParams, sortParams = true } = { } ) {
14+ export function encode ( { id, version, params, salt, hash, defaultParams, sortParams } = { } ) {
1515 if ( ! NAME_RE . test ( id ) ) throw "ID must be in the kebab-case" ;
1616
1717 var string = "$" + id ;
@@ -35,14 +35,26 @@ export function encode ( { id, version, params, salt, hash, defaultParams, sortP
3535 params = params . sort ( ( a , b ) => a [ 0 ] . localeCompare ( b [ 0 ] ) ) ;
3636 }
3737
38- for ( const [ name , value ] of params ) {
38+ let name , value ;
39+
40+ for ( [ name , value ] of params ) {
3941
4042 // name
4143 if ( ! NAME_RE . test ( name ) ) throw "Name must be in the kebab-case" ;
4244 if ( name === "v" ) throw 'Parameter name should not be a "v"' ;
4345
4446 // value
45- if ( ! VALUE_RE . test ( value ) ) throw "Parameter value is not valid" ;
47+ value ??= defaultParams ?. [ name ] ;
48+
49+ if ( value == null ) {
50+ continue ;
51+ }
52+ else if ( value instanceof Buffer ) {
53+ value = value . toString ( "base64" ) . replace ( / = + $ / , "" ) ;
54+ }
55+ else if ( ! VALUE_RE . test ( value ) ) {
56+ throw "Parameter value is not valid" ;
57+ }
4658
4759 if ( defaultParams ?. [ name ] == null || String ( defaultParams [ name ] ) !== String ( value ) ) {
4860 values . push ( name + "=" + value ) ;
0 commit comments