stringIteratorFunction,
} from "./iterable.js";
import {
+ defineOwnDataProperty,
defineOwnProperties,
getOwnPropertyDescriptors,
+ objectCreate,
+ setPropertyValues,
setPrototype,
} from "./object.js";
-import { sameValue, toLength } from "./value.js";
+import { sameValue, toLength, UNDEFINED } from "./value.js";
const RE = RegExp;
const { prototype: rePrototype } = RE;
* expression with `^(?:` and `)$` if you don’t want nongreedy
* regular expressions to fail when shorter matches are possible.
*/
- constructor(source, name = undefined, constraint = null) {
+ constructor(source, name = UNDEFINED, constraint = null) {
super(
($) => {
if (typeof $ !== "string") {
return defineOwnProperties(
setPrototype(this, matcherPrototype),
{
- lastIndex: {
+ lastIndex: setPropertyValues(objectCreate(null), {
configurable: false,
enumerable: false,
value: 0,
writable: false,
- },
- name: {
- value: name != null
+ }),
+ name: defineOwnDataProperty(
+ objectCreate(null),
+ "value",
+ name != null
? `${name}`
: `Matcher(${call(reToString, regExp, [])})`,
- },
+ ),
},
);
}
}
};
- const matcherConstructor = defineOwnProperties(
+ const matcherConstructor = Object.defineProperties(
class extends RegExp {
constructor(...args) {
return new Matcher(...args);
}
},
{
- name: { value: "Matcher" },
- length: { value: 1 },
+ name: defineOwnDataProperty(
+ Object.create(null),
+ "value",
+ "Matcher",
+ ),
+ length: defineOwnDataProperty(Object.create(null), "value", 1),
},
);
const matcherPrototype = defineOwnProperties(
matcherConstructor.prototype,
getOwnPropertyDescriptors(Matcher.prototype),
- { constructor: { value: matcherConstructor } },
+ {
+ constructor: defineOwnDataProperty(
+ Object.create(null),
+ "value",
+ matcherConstructor,
+ ),
+ },
);
return { Matcher: matcherConstructor };
*/
export const canonicalNumericIndexString = ($) => {
if (typeof $ !== "string") {
- return undefined;
+ return UNDEFINED;
} else if ($ === "-0") {
return -0;
} else {
const n = +$;
- return $ === `${n}` ? n : undefined;
+ return $ === `${n}` ? n : UNDEFINED;
}
};
export const getCharacter = ($, pos) => {
const codepoint = getCodepoint($, pos);
return codepoint == null
- ? undefined
+ ? UNDEFINED
: stringFromCodepoints(codepoint);
};
*/
getCodeUnit,
- /** Returns whether the provided value is an integer index string. */
- isIntegerIndexString,
-
/**
* Returns a string created from the provided code units.
*
const { fromCharCode } = String;
const { charCodeAt, concat } = String.prototype;
const {
- MAX_SAFE_INTEGER: MAXIMUM_SAFE_INTEGRAL_NUMBER,
isInteger: isIntegralNumber,
isNaN: isNan,
} = Number;
return {
getCodeUnit: ($, n) => {
const codeUnit = call(charCodeAt, $, [n]);
- return isNan(codeUnit) ? undefined : codeUnit;
- },
- isIntegerIndexString: ($) => {
- const value = canonicalNumericIndexString($);
- if (value !== undefined && isIntegralNumber(value)) {
- // The provided value is an integral canonical numeric index
- // string.
- return sameValue(value, 0) ||
- value > 0 && value <= MAXIMUM_SAFE_INTEGRAL_NUMBER &&
- value === toLength(value);
- } else {
- // The provided value is not an integral canonical numeric
- // index string.
- return false;
- }
+ return isNan(codeUnit) ? UNDEFINED : codeUnit;
},
- stringCatenate: defineOwnProperties(
+ stringCatenate: Object.defineProperties(
(...args) => call(concat, "", args),
{ name: { value: "stringCatenate" }, length: { value: 2 } },
),
- stringFromCodeUnits: defineOwnProperties(
+ stringFromCodeUnits: Object.defineProperties(
(...codeUnits) => {
for (let index = 0; index < codeUnits.length; ++index) {
// Iterate over each provided code unit and throw if it is
/* do nothing */
}
}
- return call(fromCharCode, undefined, codeUnits);
+ return call(fromCharCode, UNDEFINED, codeUnits);
},
{ name: { value: "stringFromCodeUnits" }, length: { value: 1 } },
),
{ name: "getLastSubstringIndex" },
);
+/** Returns whether the provided value is an array index. */
+export const isArrayIndexString = ($) => {
+ const value = canonicalNumericIndexString($);
+ if (value !== UNDEFINED) {
+ // The provided value is a canonical numeric index string; return
+ // whether it is in range for array indices.
+ return sameValue(value, 0) ||
+ value === toLength(value) && value > 0 && value < -1 >>> 0;
+ } else {
+ // The provided value is not a canonical numeric index string.
+ return false;
+ }
+};
+
+/** Returns whether the provided value is an integer index string. */
+export const isIntegerIndexString = ($) => {
+ const value = canonicalNumericIndexString($);
+ if (value !== UNDEFINED) {
+ // The provided value is a canonical numeric index string; return
+ // whether it is in range for integer indices.
+ return sameValue(value, 0) ||
+ value === toLength(value) && value > 0;
+ } else {
+ // The provided value is not a canonical numeric index string.
+ return false;
+ }
+};
+
/**
* Returns the result of joining the provided iterable.
*
call(
arrayJoin,
[...$],
- [separator === undefined ? "," : `${separator}`],
+ [separator === UNDEFINED ? "," : `${separator}`],
);
return join;
})();