Skip to content

Commit 4cab190

Browse files
authored
fix: set and construct handling strings starting with numbers (#455)
1 parent 069b26c commit 4cab190

File tree

6 files changed

+101
-8
lines changed

6 files changed

+101
-8
lines changed

cdn/radash.esm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ const set = (initial, path, value) => {
734734
const _set = (node) => {
735735
if (segments.length > 1) {
736736
const key = segments.shift();
737-
const nextIsNum = toInt(segments[0], null) === null ? false : true;
737+
const nextIsNum = /^\d+$/.test(segments[0]);
738738
node[key] = node[key] === void 0 ? nextIsNum ? [] : {} : node[key];
739739
_set(node[key]);
740740
} else {

cdn/radash.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ var radash = (function (exports) {
737737
const _set = (node) => {
738738
if (segments.length > 1) {
739739
const key = segments.shift();
740-
const nextIsNum = toInt(segments[0], null) === null ? false : true;
740+
const nextIsNum = /^\d+$/.test(segments[0]);
741741
node[key] = node[key] === void 0 ? nextIsNum ? [] : {} : node[key];
742742
_set(node[key]);
743743
} else {

cdn/radash.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "radash",
3-
"version": "12.1.0",
3+
"version": "12.1.1",
44
"description": "Functional utility library - modern, simple, typed, powerful",
55
"main": "dist/cjs/index.cjs",
66
"module": "dist/esm/index.mjs",

src/object.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { objectify } from './array'
2-
import { toInt } from './number'
32
import { isArray, isObject, isPrimitive } from './typed'
43

54
type LowercasedKeys<T extends Record<string, any>> = {
@@ -249,7 +248,7 @@ export const set = <T extends object, K>(
249248
const _set = (node: any) => {
250249
if (segments.length > 1) {
251250
const key = segments.shift() as string
252-
const nextIsNum = toInt(segments[0], null) === null ? false : true
251+
const nextIsNum = /^\d+$/.test(segments[0])
253252
node[key] = node[key] === undefined ? (nextIsNum ? [] : {}) : node[key]
254253
_set(node[key])
255254
} else {

src/tests/object.test.ts

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,40 @@ describe('object module', () => {
506506
cards: [, [, { value: 2 }]]
507507
})
508508
})
509+
510+
/**
511+
* FIX: Bug where UUID keys that start with numbers
512+
* are incorrectly treated as array indices, causing objects to be created as arrays
513+
*/
514+
test('treats uuid starting with digits as an identifier -- not an array index', () => {
515+
const result = _.set(
516+
{},
517+
'fields.754a24c1-c15b-49a2-bf37-2dc5f2b3a823.max',
518+
100
519+
)
520+
assert.deepEqual(result, {
521+
fields: {
522+
'754a24c1-c15b-49a2-bf37-2dc5f2b3a823': {
523+
max: 100
524+
}
525+
}
526+
})
527+
})
528+
529+
/**
530+
* FIX: Bug where string keys that start with numbers
531+
* are incorrectly treated as array indices, causing objects to be created as arrays
532+
*/
533+
test('treats string starting with numbers as an identifier -- not an array index', () => {
534+
const result = _.set({}, 'items.123abc.name', 'test')
535+
assert.deepEqual(result, {
536+
items: {
537+
'123abc': {
538+
name: 'test'
539+
}
540+
}
541+
})
542+
})
509543
})
510544

511545
describe('crush function', () => {
@@ -564,7 +598,7 @@ describe('object module', () => {
564598
power: 12
565599
},
566600
{
567-
name: 'vishnu',
601+
name: 'Orgo',
568602
power: 58
569603
}
570604
],
@@ -578,12 +612,72 @@ describe('object module', () => {
578612
'friend.power': 80,
579613
'enemies.0.name': 'hathor',
580614
'enemies.0.power': 12,
581-
'enemies.1.name': 'vishnu',
615+
'enemies.1.name': 'Orgo',
582616
'enemies.1.power': 58,
583617
timestamp: now
584618
}),
585619
ra
586620
)
587621
})
622+
623+
/**
624+
* FIX: Bug where objects with UUID keys that start with numbers are
625+
* ommited in the reconstructed object.
626+
*/
627+
test('returns objects with UUID keys as arrays', () => {
628+
const expected = {
629+
fields: {
630+
'754a24c1-c15b-49a2-bf37-2dc5f2b3a823': {
631+
max: 100,
632+
min: 0,
633+
type: 'number',
634+
label: 'baba123',
635+
required: true
636+
}
637+
},
638+
instructions: 'qde2ref2424'
639+
}
640+
641+
const crushed = {
642+
'fields.754a24c1-c15b-49a2-bf37-2dc5f2b3a823.max': 100,
643+
'fields.754a24c1-c15b-49a2-bf37-2dc5f2b3a823.min': 0,
644+
'fields.754a24c1-c15b-49a2-bf37-2dc5f2b3a823.type': 'number',
645+
'fields.754a24c1-c15b-49a2-bf37-2dc5f2b3a823.label': 'baba123',
646+
'fields.754a24c1-c15b-49a2-bf37-2dc5f2b3a823.required': true,
647+
instructions: 'qde2ref2424'
648+
}
649+
650+
const result = _.construct(crushed)
651+
assert.deepEqual(result, expected)
652+
})
653+
654+
/**
655+
* FIX: Bug where objects with string keys that start with numbers are
656+
* ommited in the reconstructed object.
657+
*/
658+
test('returns objects with alphanumeric keys as arrays', () => {
659+
const expected = {
660+
items: {
661+
'123abc': {
662+
name: 'test',
663+
enabled: true
664+
},
665+
'456def': {
666+
name: 'another',
667+
enabled: false
668+
}
669+
}
670+
}
671+
672+
const crushed = {
673+
'items.123abc.name': 'test',
674+
'items.123abc.enabled': true,
675+
'items.456def.name': 'another',
676+
'items.456def.enabled': false
677+
}
678+
679+
const result = _.construct(crushed)
680+
assert.deepEqual(result, expected)
681+
})
588682
})
589683
})

0 commit comments

Comments
 (0)