-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(justjs): Replaced justjs with vanilla/*.js
test(StateService): Add state service tests from angular-ui-router test(*): Use vanilla services and hashBangLocation for tests chore(build): Use typescript@rc for async/await in tests
- Loading branch information
1 parent
bbd7d25
commit 241d972
Showing
20 changed files
with
1,019 additions
and
147 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
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 @@ | ||
export * from "./vanilla/index"; |
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 { | ||
extend, assertPredicate, isFunction, isArray, isInjectable, $InjectorLike, IInjectable | ||
} from "../common/module"; | ||
|
||
/** angular1-like injector api */ | ||
// globally available injectables | ||
let globals = {}; | ||
let STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; | ||
let ARGUMENT_NAMES = /([^\s,]+)/g; | ||
|
||
export const $injector = { | ||
get: name => globals[name], | ||
|
||
has: (name) => $injector.get(name) != null, | ||
|
||
invoke: (fn: IInjectable, context?, locals?) => { | ||
let all = extend({}, globals, locals || {}); | ||
let params = $injector.annotate(fn); | ||
let ensureExist = assertPredicate(key => all.hasOwnProperty(key), key => `DI can't find injectable: '${key}'`); | ||
let args = params.filter(ensureExist).map(x => all[x]); | ||
if (isFunction(fn)) return fn.apply(context, args); | ||
else return (fn as any[]).slice(-1)[0].apply(context, args); | ||
}, | ||
|
||
annotate: (fn: IInjectable): any[] => { | ||
if (!isInjectable(fn)) throw new Error(`Not an injectable function: ${fn}`); | ||
if (fn && (fn as any).$inject) return (fn as any).$inject; | ||
if (isArray(fn)) return fn.slice(0, -1); | ||
let fnStr = fn.toString().replace(STRIP_COMMENTS, ''); | ||
let result = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(ARGUMENT_NAMES); | ||
return result || []; | ||
} | ||
} as $InjectorLike; |
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,37 @@ | ||
import { isArray, isObject, $QLike } from "../common/module"; | ||
|
||
/** $q-like promise api */ | ||
export const $q = { | ||
when: (val) => new Promise((resolve, reject) => resolve(val)), | ||
|
||
reject: (val) => new Promise((resolve, reject) => { reject(val); }), | ||
|
||
defer: () => { | ||
let deferred: any = {}; | ||
deferred.promise = new Promise((resolve, reject) => { | ||
deferred.resolve = resolve; | ||
deferred.reject = reject; | ||
}); | ||
return deferred; | ||
}, | ||
|
||
all: (promises: { [key: string]: Promise<any> } | Promise<any>[]) => { | ||
if (isArray(promises)) { | ||
return new Promise((resolve, reject) => { | ||
let results = []; | ||
promises.reduce((wait4, promise) => wait4.then(() => promise.then(val => results.push(val))), $q.when()) | ||
.then(() => { resolve(results); }, reject); | ||
}); | ||
} | ||
|
||
if (isObject(promises)) { | ||
// Convert promises map to promises array. | ||
// When each promise resolves, map it to a tuple { key: key, val: val } | ||
let chain = Object.keys(promises) | ||
.map(key => promises[key].then(val => ({key, val}))); | ||
// Then wait for all promises to resolve, and convert them back to an object | ||
return $q.all(chain).then(values => | ||
values.reduce((acc, tuple) => { acc[tuple.key] = tuple.val; return acc; }, {})); | ||
} | ||
} | ||
} as $QLike; |
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,52 @@ | ||
import { services, isDefined } from '../common/module'; | ||
import { LocationConfig, LocationServices } from '../common/coreservices'; | ||
import { HistoryImplementation } from './interface'; | ||
import { splitQuery, trimHashVal, getParams } from './utils'; | ||
|
||
let hashPrefix: string = ''; | ||
let baseHref: string = ''; | ||
|
||
const locationServiceConfig: LocationConfig = { | ||
port: () => | ||
parseInt(location.port), | ||
protocol: () => | ||
location.protocol, | ||
host: () => | ||
location.host, | ||
baseHref: () => | ||
baseHref, | ||
html5Mode: () => | ||
true, | ||
hashPrefix: (newprefix?: string): string => { | ||
if (isDefined(newprefix)) { | ||
hashPrefix = newprefix; | ||
} | ||
return hashPrefix; | ||
} | ||
}; | ||
|
||
const locationService: LocationServices = { | ||
hash: () => | ||
trimHashVal(location.hash), | ||
path: () => { | ||
let base = services.locationConfig.baseHref(); | ||
let path = location.pathname; | ||
let idx = path.indexOf(base); | ||
if (idx !== 0) throw new Error(`current url: ${path} does not start with <base> tag ${base}`); | ||
return path.substr(base.length); | ||
}, | ||
search: () => | ||
getParams(splitQuery(location.search)[1]), | ||
setUrl: (url: string, replace: boolean = false) => { | ||
if (isDefined(url)) { | ||
if (replace) history.replaceState(null, null, services.locationConfig.baseHref() + url); | ||
else history.pushState(null, null, services.locationConfig.baseHref() + url); | ||
} | ||
}, | ||
onChange: (cb: EventListener) => window.addEventListener("popstate", cb, false) as any | ||
}; | ||
|
||
export const browserHistory: HistoryImplementation = { | ||
service: locationService, | ||
configuration: locationServiceConfig | ||
}; |
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,44 @@ | ||
import {services, isDefined} from '../common/module'; | ||
import {LocationConfig, LocationServices} from '../common/coreservices'; | ||
import {HistoryImplementation} from './interface'; | ||
import {splitHash, splitQuery, trimHashVal, getParams} from './utils'; | ||
|
||
let hashPrefix: string = ''; | ||
let baseHref: string = ''; | ||
|
||
const locationServiceConfig: LocationConfig = { | ||
port: () => | ||
parseInt(location.port), | ||
protocol: () => | ||
location.protocol, | ||
host: () => | ||
location.host, | ||
baseHref: () => | ||
baseHref, | ||
html5Mode: () => | ||
false, | ||
hashPrefix: (newprefix?: string): string => { | ||
if(isDefined(newprefix)) { | ||
hashPrefix = newprefix; | ||
} | ||
return hashPrefix; | ||
} | ||
} | ||
|
||
const locationService: LocationServices = { | ||
hash: () => | ||
splitHash(trimHashVal(location.hash))[1], | ||
path: () => | ||
splitHash(splitQuery(trimHashVal(location.hash))[0])[0], | ||
search: () => | ||
getParams(splitQuery(splitHash(trimHashVal(location.hash))[0])[1]), | ||
setUrl: (url: string, replace: boolean = true) => { | ||
if (url) location.hash = url; | ||
}, | ||
onChange: (cb: EventListener) => window.addEventListener("hashchange", cb, false) as any | ||
} | ||
|
||
export const hashHistory: HistoryImplementation = { | ||
service: locationService, | ||
configuration: locationServiceConfig | ||
} |
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,38 @@ | ||
/** | ||
* Naive, pure JS implementation of core ui-router services | ||
* | ||
* @module vanilla | ||
*/ /** */ | ||
import { UIRouter } from "../router"; | ||
|
||
import { services as coreservices } from "../common/coreservices"; | ||
import { $q } from "./$q"; | ||
import { $injector } from "./$injector"; | ||
import { hashHistory } from "./hashHistory"; | ||
import { browserHistory } from "./browserHistory"; | ||
import { HistoryImplementationPlugin, ServicesPlugin, HistoryImplementation } from "./interface"; | ||
import { extend } from "../common/common"; | ||
|
||
export { $q, $injector, hashHistory, browserHistory }; | ||
|
||
export function services(router: UIRouter): ServicesPlugin { | ||
coreservices.$injector = $injector; | ||
coreservices.$q = $q; | ||
|
||
return { name: "vanilla.services", $q, $injector }; | ||
} | ||
|
||
const HistoryImplementationPluginFactory = (name: string, historyImpl: HistoryImplementation) => | ||
(router: UIRouter) => { | ||
const { service, configuration } = historyImpl; | ||
extend(coreservices.location, service); | ||
extend(coreservices.locationConfig, configuration); | ||
|
||
return { name, service, configuration }; | ||
}; | ||
|
||
export const hashLocation: (router: UIRouter) => HistoryImplementationPlugin = | ||
HistoryImplementationPluginFactory("vanilla.hashBangLocation", hashHistory); | ||
export const pushStateLocation: (router: UIRouter) => HistoryImplementationPlugin = | ||
HistoryImplementationPluginFactory("vanilla.pushStateLocation", browserHistory); | ||
|
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,17 @@ | ||
import { LocationConfig, LocationServices } from '../common/coreservices'; | ||
import { UIRouterPlugin } from "../interface"; | ||
import { $InjectorLike, $QLike } from "../common/module"; | ||
|
||
export interface HistoryImplementation { | ||
service: LocationServices; | ||
configuration: LocationConfig; | ||
} | ||
|
||
export interface HistoryImplementationPlugin extends UIRouterPlugin, HistoryImplementation { | ||
|
||
} | ||
|
||
export interface ServicesPlugin extends UIRouterPlugin { | ||
$q: $QLike, | ||
$injector: $InjectorLike | ||
} |
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,27 @@ | ||
import {isArray} from "../common/module"; | ||
|
||
const beforeAfterSubstr = (char: string) => (str: string): string[] => { | ||
if (!str) return ["", ""]; | ||
let idx = str.indexOf(char); | ||
if (idx === -1) return [str, ""]; | ||
return [str.substr(0, idx), str.substr(idx + 1)]; | ||
}; | ||
|
||
export const splitHash = beforeAfterSubstr("#"); | ||
export const splitQuery = beforeAfterSubstr("?"); | ||
export const splitEqual = beforeAfterSubstr("="); | ||
export const trimHashVal = (str):string => str ? str.replace(/^#/, "") : ""; | ||
|
||
export const keyValsToObjectR = (accum, [key, val]) => { | ||
if (!accum.hasOwnProperty(key)) { | ||
accum[key] = val; | ||
} else if (isArray(accum[key])) { | ||
accum[key].push(val); | ||
} else { | ||
accum[key] = [accum[key], val] | ||
} | ||
return accum; | ||
}; | ||
|
||
export const getParams = (queryString: string): any => | ||
queryString.split("&").map(splitEqual).reduce(keyValsToObjectR, {}); |
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
Oops, something went wrong.