-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16523 from emberjs/more-ts
Convert ember-environment to typescript.
- Loading branch information
Showing
8 changed files
with
135 additions
and
92 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,3 @@ | ||
export * from './lib/context'; | ||
export * from './lib/env'; | ||
export * from './lib/has-dom'; |
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,32 @@ | ||
import global from './global'; | ||
|
||
export interface GlobalContext { | ||
imports: object; | ||
exports: object; | ||
lookup: object; | ||
} | ||
|
||
// legacy imports/exports/lookup stuff (should we keep this??) | ||
export const context = (function( | ||
global: object, | ||
Ember: Partial<GlobalContext> | undefined | ||
): GlobalContext { | ||
return Ember === undefined | ||
? { imports: global, exports: global, lookup: global } | ||
: { | ||
// import jQuery | ||
imports: Ember.imports || global, | ||
// export Ember | ||
exports: Ember.exports || global, | ||
// search for Namespaces | ||
lookup: Ember.lookup || global, | ||
}; | ||
})(global, global.Ember); | ||
|
||
export function getLookup(): object { | ||
return context.lookup; | ||
} | ||
|
||
export function setLookup(value: object): void { | ||
context.lookup = value; | ||
} |
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
7 changes: 4 additions & 3 deletions
7
packages/ember-environment/lib/global.js → packages/ember-environment/lib/global.ts
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 |
---|---|---|
@@ -1,18 +1,19 @@ | ||
/* globals global, window, self, mainContext */ | ||
declare const mainContext: object | undefined; | ||
|
||
// from lodash to catch fake globals | ||
function checkGlobal(value) { | ||
function checkGlobal(value: any | null | undefined): value is object { | ||
return value && value.Object === Object ? value : undefined; | ||
} | ||
|
||
// element ids can ruin global miss checks | ||
function checkElementIdShadowing(value) { | ||
function checkElementIdShadowing(value: any | null | undefined) { | ||
return value && value.nodeType === undefined ? value : undefined; | ||
} | ||
|
||
// export real global | ||
export default checkGlobal(checkElementIdShadowing(typeof global === 'object' && global)) || | ||
checkGlobal(typeof self === 'object' && self) || | ||
checkGlobal(typeof window === 'object' && window) || | ||
mainContext || // set before strict mode in Ember loader/wrapper | ||
(typeof mainContext !== 'undefined' && mainContext) || // set before strict mode in Ember loader/wrapper | ||
new Function('return this')(); // eval outside of strict mode |
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,33 @@ | ||
import { ENV } from './env'; | ||
import global from './global'; | ||
|
||
// check if window exists and actually is the global | ||
const hasDOM = | ||
typeof window !== 'undefined' && | ||
window === global && | ||
window.document && | ||
window.document.createElement && | ||
!(ENV as any).disableBrowserEnvironment; // is this a public thing? | ||
|
||
declare const InstallTrigger: any | undefined; | ||
|
||
// TODO: cleanup single source of truth issues with this stuff | ||
export const environment = hasDOM | ||
? { | ||
hasDOM: true, | ||
isChrome: !!(window as any).chrome && !(window as any).opera, | ||
isFirefox: typeof InstallTrigger !== 'undefined', | ||
location: window.location, | ||
history: window.history, | ||
userAgent: window.navigator.userAgent, | ||
window, | ||
} | ||
: { | ||
hasDOM: false, | ||
isChrome: false, | ||
isFirefox: false, | ||
location: null, | ||
history: null, | ||
userAgent: 'Lynx (textmode)', | ||
window: null, | ||
}; |
This file was deleted.
Oops, something went wrong.
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,21 @@ | ||
export function defaultTrue(v: any | undefined | null) { | ||
return v === false ? false : true; | ||
} | ||
|
||
export function defaultFalse(v: any | undefined | null) { | ||
return v === true ? true : false; | ||
} | ||
|
||
export function normalizeExtendPrototypes(obj: any | undefined | null) { | ||
if (obj === false) { | ||
return { String: false, Array: false, Function: false }; | ||
} | ||
if (obj === undefined || obj === null || obj === true) { | ||
return { String: true, Array: true, Function: true }; | ||
} | ||
return { | ||
String: defaultTrue(obj.String), | ||
Array: defaultTrue(obj.Array), | ||
Function: defaultTrue(obj.Function), | ||
}; | ||
} |