seroval 0.4.0-alpha.5 → 0.4.0-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -145,7 +145,7 @@ function createMapSet(
145
145
  function createArrayAssign(
146
146
  ctx: SerializationContext,
147
147
  ref: number,
148
- index: number,
148
+ index: number | string,
149
149
  value: string,
150
150
  ) {
151
151
  markRef(ctx, ref);
@@ -157,11 +157,9 @@ function createObjectAssign(
157
157
  ref: number,
158
158
  key: string,
159
159
  value: string,
160
- computed: boolean,
161
160
  ) {
162
161
  markRef(ctx, ref);
163
- const member = computed ? `[${key}]` : `.${key}`;
164
- createAssignment(ctx, `${getRefParam(ctx, ref)}${member}`, value);
162
+ createAssignment(ctx, `${getRefParam(ctx, ref)}.${key}`, value);
165
163
  }
166
164
 
167
165
  const IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
@@ -185,13 +183,11 @@ function serializeAssignments(
185
183
  // Test if key is a valid number or JS identifier
186
184
  // so that we don't have to serialize the key and wrap with brackets
187
185
  const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
188
- createObjectAssign(
189
- ctx,
190
- targetRef,
191
- isIdentifier ? key : quote(key),
192
- refParam,
193
- !(isIdentifier && Number.isNaN(check)),
194
- );
186
+ if (isIdentifier && Number.isNaN(check)) {
187
+ createObjectAssign(ctx, targetRef, key, refParam);
188
+ } else {
189
+ createArrayAssign(ctx, targetRef, isIdentifier ? key : quote(key), refParam);
190
+ }
195
191
  ctx.assignments = parentAssignment;
196
192
  }
197
193
  ctx.stack.pop();
@@ -215,18 +211,15 @@ function serializeObject(
215
211
  // Test if key is a valid number or JS identifier
216
212
  // so that we don't have to serialize the key and wrap with brackets
217
213
  const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
218
- const validKey = isIdentifier ? key : quote(key);
219
214
  if (isReferenceInStack(ctx, val)) {
220
215
  const refParam = getRefParam(ctx, val[1]);
221
- createObjectAssign(
222
- ctx,
223
- sourceID,
224
- validKey,
225
- refParam,
226
- !(isIdentifier && Number.isNaN(check)),
227
- );
216
+ if (isIdentifier && Number.isNaN(check)) {
217
+ createObjectAssign(ctx, sourceID, key, refParam);
218
+ } else {
219
+ createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
220
+ }
228
221
  } else {
229
- result += `${validKey}:${serializeTree(ctx, val)},`;
222
+ result += `${isIdentifier ? key : quote(key)}:${serializeTree(ctx, val)},`;
230
223
  }
231
224
  }
232
225
  ctx.stack.pop();