Skip to content

Commit

Permalink
Use NullObject, restore isDate
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Oct 7, 2024
1 parent 5a5cb2c commit eae5c49
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ const domainValueRegExp =
*/
const pathValueRegExp = /^[\u0020-\u003A\u003D-\u007E]*$/;

const __toString = Object.prototype.toString;
const __hasOwnProperty = Object.prototype.hasOwnProperty;

const NullObject = function () {};
NullObject.prototype = Object.create(null);

/**
* Parse options.
*/
Expand Down Expand Up @@ -86,7 +92,7 @@ export function parse(
str: string,
options?: ParseOptions,
): Record<string, string> {
const obj: Record<string, string> = {};
const obj: Record<string, string> = new NullObject();

Check failure on line 95 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.

Check failure on line 95 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 22

'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
const len = str.length;
// RFC 6265 sec 4.1.1, RFC 2616 2.2 defines a cookie name consists of one char minimum, plus '='.
if (len < 2) return obj;
Expand All @@ -112,7 +118,7 @@ export function parse(
const key = str.slice(keyStartIdx, keyEndIdx);

// only assign once
if (!obj.hasOwnProperty(key)) {
if (!__hasOwnProperty.call(obj, key)) {
let valStartIdx = startIndex(str, eqIdx + 1, endIdx);
let valEndIdx = endIndex(str, endIdx, valStartIdx);

Expand Down Expand Up @@ -296,7 +302,10 @@ export function serialize(
}

if (options.expires) {
if (!Number.isFinite(options.expires.valueOf())) {
if (
!isDate(options.expires) ||
!Number.isFinite(options.expires.valueOf())
) {
throw new TypeError("option expires is invalid");
}

Expand Down Expand Up @@ -358,6 +367,13 @@ function decode(str: string): string {
return str.indexOf("%") !== -1 ? decodeURIComponent(str) : str;
}

/**
* Determine if value is a Date.
*/
function isDate(val: any): val is Date {
return __toString.call(val) === "[object Date]";
}

/**
* Try decoding a string using a decoding function.
*/
Expand Down

0 comments on commit eae5c49

Please sign in to comment.