From 6d1763160bcb545a9f46b2a27b3d11e47ee426d7 Mon Sep 17 00:00:00 2001 From: Chris Garrett Date: Thu, 27 Feb 2020 13:11:55 -0800 Subject: [PATCH] add suggestion --- .../-internals/utils/lib/mandatory-setter.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/@ember/-internals/utils/lib/mandatory-setter.ts b/packages/@ember/-internals/utils/lib/mandatory-setter.ts index fe9c4b489f4..e23df101b17 100644 --- a/packages/@ember/-internals/utils/lib/mandatory-setter.ts +++ b/packages/@ember/-internals/utils/lib/mandatory-setter.ts @@ -14,6 +14,19 @@ export let setWithMandatorySetter: type PropertyDescriptorWithMeta = PropertyDescriptor & { hadOwnProperty?: boolean }; +function isElementKey(key: string | number | symbol) { + return typeof key === 'number' ? isPositiveInt(key) : isStringInt(key as string); +} + +function isStringInt(str: string) { + let num = parseInt(str, 10); + return isPositiveInt(num) && str === String(num); +} + +function isPositiveInt(num: number) { + return num >= 0 && num % 1 === 0; +} + if (DEBUG) { let SEEN_TAGS = new WeakSet(); @@ -34,9 +47,8 @@ if (DEBUG) { SEEN_TAGS!.add(tag); - if (Array.isArray(obj) && !isNaN(Number(keyName))) { - // don't setup mandatory setter, arrays can remove property descriptors - // themselves and we can't control them correctly. + if (Array.isArray(obj) && !isElementKey(keyName)) { + // this would replace the conditional return; }