-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(IE9): Add safeConsole so IE9 doesn't break
It's 2020 I can't believe I'm adding IE9 compat :)
- Loading branch information
1 parent
243e548
commit 9c8579d
Showing
3 changed files
with
51 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** workaround for missing console object in IE9 when dev tools haven't been opened o_O */ | ||
/* tslint:disable:no-console */ | ||
import { noop } from './common'; | ||
|
||
const noopConsoleStub = { log: noop, error: noop, table: noop }; | ||
|
||
function ie9Console(console) { | ||
const bound = (fn: Function) => Function.prototype.bind.call(fn, console); | ||
return { | ||
log: bound(console.log), | ||
error: bound(console.log), | ||
table: bound(console.log), | ||
}; | ||
} | ||
|
||
function fallbackConsole(console) { | ||
const log = console.log.bind(console); | ||
const error = console.error ? console.error.bind(console) : log; | ||
const table = console.table ? console.table.bind(console) : log; | ||
return { log, error, table }; | ||
} | ||
|
||
function getSafeConsole() { | ||
// @ts-ignore | ||
const isIE9 = document && document.documentMode && document.documentMode === 9; | ||
if (isIE9) { | ||
return window && window.console ? ie9Console(window.console) : noopConsoleStub; | ||
} else if (!console.table || !console.error) { | ||
return fallbackConsole(console); | ||
} else { | ||
return console; | ||
} | ||
} | ||
|
||
export const safeConsole = getSafeConsole(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters