Skip to content

Commit 701703f

Browse files
committed
fix: unicode keys in JSON becoming empty (#164)
1 parent b4fcf84 commit 701703f

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/components/editor/converter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { compact, uniqueId } from 'lodash-es'
22
import { DeepPartial, FieldArrayWithId } from 'react-hook-form'
3-
import snakeCaseKeys from 'snakecase-keys'
43

54
import type { CopilotDocV1 } from 'models/copilot.schema'
65
import { MinimumRequired } from 'models/operation'
76

87
import { findOperatorDirection } from '../../models/operator'
98
import { findActionType } from '../../models/types'
9+
import { snakeCaseKeysUnicode } from '../../utils/object'
1010

1111
/**
1212
* Creates an operation that can be used in editor. Used for importing.
@@ -75,7 +75,7 @@ export function toMaaOperation(
7575
delete (item as Partial<FieldArrayWithId>).id
7676
})
7777

78-
return snakeCaseKeys(operation, { deep: true })
78+
return snakeCaseKeysUnicode(operation, { deep: true })
7979
}
8080

8181
/**

src/components/viewer/OperationViewer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { useAtom } from 'jotai'
2222
import { noop } from 'lodash-es'
2323
import { ComponentType, FC, useEffect, useMemo, useState } from 'react'
2424
import { Link } from 'react-router-dom'
25-
import snakecaseKeys from 'snakecase-keys'
2625

2726
import { FactItem } from 'components/FactItem'
2827
import { Paragraphs } from 'components/Paragraphs'
@@ -45,6 +44,7 @@ import { createCustomLevel, findLevelByStageName } from '../../models/level'
4544
import { Level } from '../../models/operation'
4645
import { toShortCode } from '../../models/shortCode'
4746
import { formatError } from '../../utils/error'
47+
import { snakeCaseKeysUnicode } from '../../utils/object'
4848
import { ActionCard } from '../ActionCard'
4949
import { CommentArea } from './comment/CommentArea'
5050

@@ -154,7 +154,7 @@ export const OperationViewer: ComponentType<{
154154
const handleDownloadJSON = () => {
155155
// pretty print the JSON
156156
const json = JSON.stringify(
157-
snakecaseKeys(operationDoc, { deep: true }),
157+
snakeCaseKeysUnicode(operationDoc, { deep: true }),
158158
null,
159159
2,
160160
)

src/utils/object.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import snakeCaseKeys from 'snakecase-keys'
2+
3+
export const snakeCaseKeysUnicode = ((
4+
input: any,
5+
options?: snakeCaseKeys.Options,
6+
) => {
7+
// a regex that is supposed to match nothing, which prevents `snake-case` from
8+
// using its default regex to strip out non-word characters (including non ASCII characters)
9+
// see: https://github.com/blakeembrey/change-case/blob/040a079f007879cb0472ba4f7cc2e1d3185e90ba/packages/no-case/src/index.ts#L14
10+
const unmatchableRegex = /^\x00\x01/ // eslint-disable-line no-control-regex
11+
12+
return snakeCaseKeys(input, {
13+
...options,
14+
parsingOptions: {
15+
...options?.parsingOptions,
16+
stripRegexp: unmatchableRegex,
17+
},
18+
})
19+
}) as typeof snakeCaseKeys

0 commit comments

Comments
 (0)