From 25c0d104a93743bfe407e4ed6a239fb5b9a36149 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Thu, 14 Jan 2021 15:25:57 -0600 Subject: [PATCH] convert macOS check to utility function --- src/components/Editor.tsx | 5 +++-- src/components/Terminal.tsx | 5 +++-- src/utils/os.test.ts | 15 +++++++++++++++ src/utils/os.ts | 8 ++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 src/utils/os.test.ts create mode 100644 src/utils/os.ts diff --git a/src/components/Editor.tsx b/src/components/Editor.tsx index 25038573..80fde2cd 100644 --- a/src/components/Editor.tsx +++ b/src/components/Editor.tsx @@ -17,6 +17,7 @@ import { connect } from 'react-redux'; import { Action, Dispatch } from '../actions'; import { setEditSession, storageChanged } from '../actions/editor'; import { RootState } from '../reducers'; +import { isMacOS } from '../utils/os'; import { EditorStringId } from './editor-i18n'; import en from './editor-i18n.en.json'; @@ -142,7 +143,7 @@ class Editor extends React.Component { }} text={i18n.translate(EditorStringId.Copy)} icon="duplicate" - label={/mac/i.test(navigator.platform) ? 'Cmd-C' : 'Ctrl-C'} + label={isMacOS() ? 'Cmd-C' : 'Ctrl-C'} disabled={this.editor?.getSelection().isEmpty()} /> { }} text={i18n.translate(EditorStringId.Paste)} icon="clipboard" - label={/mac/i.test(navigator.platform) ? 'Cmd-V' : 'Ctrl-V'} + label={isMacOS() ? 'Cmd-V' : 'Ctrl-V'} /> { }} text={i18n.translate(TerminalStringId.Copy)} icon="duplicate" - label={/mac/i.test(navigator.platform) ? 'Cmd-C' : 'Ctrl-Shift-C'} + label={isMacOS() ? 'Cmd-C' : 'Ctrl-Shift-C'} disabled={!this.xterm.hasSelection()} /> { }} text={i18n.translate(TerminalStringId.Paste)} icon="clipboard" - label={/mac/i.test(navigator.platform) ? 'Cmd-V' : 'Ctrl-V'} + label={isMacOS() ? 'Cmd-V' : 'Ctrl-V'} /> { + test('is true', () => { + jest.spyOn(navigator, 'platform', 'get').mockReturnValue('MacIntel'); + expect(isMacOS()).toBeTruthy(); + }); + test('is false', () => { + jest.spyOn(navigator, 'platform', 'get').mockReturnValue('Win32'); + expect(isMacOS()).toBeFalsy(); + }); +}); diff --git a/src/utils/os.ts b/src/utils/os.ts new file mode 100644 index 00000000..44a11446 --- /dev/null +++ b/src/utils/os.ts @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2021 The Pybricks Authors + +// Utility functions for dealing with operating systems. + +export function isMacOS(): boolean { + return /mac/i.test(navigator.platform); +}