Skip to content

Commit f9d6864

Browse files
authored
feat(shared): sharable symbol (#1261)
1 parent 56cf660 commit f9d6864

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

packages/shared/src/utils.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,8 @@ export function format(message: string, ...args: any): string {
4848
)
4949
}
5050

51-
const hasSymbol =
52-
typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol'
53-
54-
export const makeSymbol = (name: string): symbol | string =>
55-
hasSymbol ? Symbol(name) : name
51+
export const makeSymbol = (name: string, shareable = false): symbol =>
52+
!shareable ? Symbol(name) : Symbol.for(name)
5653

5754
export const generateFormatCacheKey = (
5855
locale: string,

packages/shared/test/utils.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { format, generateCodeFrame } from '../src/index'
1+
import { format, generateCodeFrame, makeSymbol } from '../src/index'
22

33
test('format', () => {
44
expect(format(`foo: {0}`, 'x')).toEqual('foo: x')
@@ -12,3 +12,8 @@ test('generateCodeFrame', () => {
1212
const keyEnd = keyStart + `{ 'kazupon' }`.length
1313
expect(generateCodeFrame(source, keyStart, keyEnd)).toMatchSnapshot()
1414
})
15+
16+
test('makeSymbol', () => {
17+
expect(makeSymbol('foo')).not.toEqual(makeSymbol('foo'))
18+
expect(makeSymbol('bar', true)).toEqual(makeSymbol('bar', true))
19+
})

0 commit comments

Comments
 (0)