This repository has been archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Josh Goldberg
committed
Jun 11, 2017
0 parents
commit 1cdaddf
Showing
11 changed files
with
488 additions
and
0 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,11 @@ | ||
dist/** | ||
docs/generated/** | ||
lib/** | ||
*.css | ||
*.d.ts | ||
*.js* | ||
!gulpfile.js | ||
*.html | ||
node_modules/ | ||
npm-debug.log | ||
debug.log |
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 @@ | ||
node_modules/ | ||
test/**/* | ||
npm-debug.log |
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,8 @@ | ||
language: node_js | ||
|
||
node_js: | ||
- "7" | ||
- "5" | ||
|
||
script: | ||
node_modules/gulp/bin/gulp.js |
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,18 @@ | ||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,15 @@ | ||
<!-- {{Top}} --> | ||
# featureboxr | ||
[](https://travis-ci.org/FullScreenShenanigans/featureboxr) | ||
[](http://badge.fury.io/js/featureboxr) | ||
|
||
Gates features behind generational gaps. | ||
<!-- {{/Top}} --> | ||
|
||
<!-- {{Development}} --> | ||
## Development | ||
|
||
See [Documentation/Development](https://github.com/FullScreenShenanigans/Documentation). | ||
|
||
|
||
<!-- {{/Development}} --> |
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 @@ | ||
require("gulp-shenanigans").initialize(require("gulp")); |
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,95 @@ | ||
import { IFeatureBoxr, IFeatureBoxrSettings, IGenerations } from "./IFeatureBoxr"; | ||
|
||
/** | ||
* Creates a get-only version of a matched features object. | ||
* | ||
* @type TFeatures Generation-variant features. | ||
* @param matchedFeatures Matched features for a generation. | ||
* @returns A get-only version of the matched features object. | ||
*/ | ||
const generateGettableFeatures = <TFeatures>(matchedFeatures: Partial<TFeatures>): TFeatures => { | ||
const features = {} as TFeatures; | ||
|
||
for (const featureName in matchedFeatures) { | ||
Object.defineProperty(features, featureName, { | ||
get() { | ||
return matchedFeatures[featureName]; | ||
} | ||
}); | ||
} | ||
|
||
console.log( | ||
"Computed", | ||
Object.keys(matchedFeatures), | ||
Object.keys(matchedFeatures).map((key) => (features as any)[key])); | ||
return features; | ||
}; | ||
|
||
/** | ||
* Gates features behind generational gaps. | ||
* | ||
* @type TFeatures Generation-variant features. | ||
*/ | ||
export class FeatureBoxr<TFeatures> implements IFeatureBoxr<TFeatures> { | ||
/** | ||
* Generation-variant features. | ||
*/ | ||
public get features(): TFeatures { | ||
return this.cachedFeatures; | ||
} | ||
|
||
/** | ||
* Groups of feature settings, in order. | ||
*/ | ||
private readonly generations: IGenerations<TFeatures>; | ||
|
||
/** | ||
* Names of the available feature boxes. | ||
*/ | ||
private readonly generationNames: string[]; | ||
|
||
/** | ||
* Feature availabilities cached by this.reset. | ||
*/ | ||
private cachedFeatures: TFeatures; | ||
|
||
/** | ||
* Initializes a new instance of the FeatureBoxr class. | ||
* | ||
* @param settings Settings to be used for initialization. | ||
*/ | ||
public constructor(settings: IFeatureBoxrSettings<TFeatures>) { | ||
this.generations = settings.generations; | ||
this.generationNames = Object.keys(this.generations); | ||
|
||
this.setGeneration(settings.generation || Object.keys(this.generations)[0]); | ||
} | ||
|
||
/** | ||
* Sets features to a generation. | ||
* | ||
* @param generation Generation for feature availability. | ||
*/ | ||
public setGeneration(generationName: string): void { | ||
const indexOf: number = this.generationNames.indexOf(generationName); | ||
console.log("eyy", this.generationNames); | ||
|
||
if (indexOf === -1) { | ||
throw new Error(`Unknown generation: '${generationName}'.`); | ||
} | ||
|
||
const matchedFeatures: Partial<TFeatures> = {}; | ||
|
||
for (let i: number = 0; i <= indexOf; i += 1) { | ||
const generation = this.generations[this.generationNames[i]]; | ||
console.log("From", generation); | ||
|
||
for (const featureName in generation) { | ||
console.log("\tfeatureName", featureName, generation[featureName]); | ||
matchedFeatures[featureName] = generation[featureName]; | ||
} | ||
} | ||
|
||
this.cachedFeatures = generateGettableFeatures(matchedFeatures); | ||
} | ||
} |
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,26 @@ | ||
export interface IGenerations<TFeatures> { | ||
[i: string]: Partial<TFeatures>; | ||
} | ||
|
||
/** | ||
* Settings to initialize a new instance of the FeatureBoxr class. | ||
* | ||
* @type TFeatures Generation-variant features. | ||
*/ | ||
export interface IFeatureBoxrSettings<TFeatures> { | ||
/** | ||
* Groups of feature settings, in order. | ||
*/ | ||
generations: IGenerations<TFeatures>; | ||
|
||
/** | ||
* Starting generation to enable, if not the first keyed. | ||
*/ | ||
generation?: string; | ||
} | ||
|
||
export interface IFeatureBoxr<TFeatures> { | ||
readonly features: TFeatures; | ||
|
||
setGeneration(generationName: string): void; | ||
} |
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,99 @@ | ||
import { FeatureBoxr } from "../src/featureBoxr"; | ||
import { mochaLoader } from "./main"; | ||
|
||
const [first, second, value] = ["first", "second", "value"]; | ||
|
||
mochaLoader.it("gets a feature setting from a first generation", (): void => { | ||
// Arrange | ||
const featureBoxer = new FeatureBoxr<{ value: string }>({ | ||
generations: { | ||
[first]: { value } | ||
} | ||
}); | ||
|
||
// Act | ||
const currentValue = featureBoxer.features.value; | ||
|
||
// Assert | ||
chai.expect(currentValue).to.be.equal(value); | ||
}); | ||
|
||
mochaLoader.it("gets a feature setting from a second generation", (): void => { | ||
// Arrange | ||
const featureBoxer = new FeatureBoxr<{ value: string }>({ | ||
generations: { | ||
[first]: {}, | ||
[second]: { value } | ||
}, | ||
generation: second | ||
}); | ||
|
||
// Act | ||
const currentValue = featureBoxer.features.value; | ||
|
||
// Assert | ||
chai.expect(currentValue).to.be.equal(value); | ||
}); | ||
|
||
mochaLoader.it("gets an overridden feature setting from a second generation", (): void => { | ||
// Arrange | ||
const featureBoxer = new FeatureBoxr<{ value: string }>({ | ||
generations: { | ||
[first]: { | ||
[value]: "wrong" | ||
}, | ||
[second]: { value } | ||
}, | ||
generation: second | ||
}); | ||
|
||
// Act | ||
const currentValue = featureBoxer.features.value; | ||
|
||
// Assert | ||
chai.expect(currentValue).to.be.equal(value); | ||
}); | ||
|
||
mochaLoader.it("gets an first feature setting from resetting to a first generation", (): void => { | ||
// Arrange | ||
const featureBoxer = new FeatureBoxr<{ value: string }>({ | ||
generations: { | ||
[first]: { | ||
value: first | ||
}, | ||
[second]: { | ||
value: second | ||
} | ||
}, | ||
generation: second | ||
}); | ||
|
||
// Act | ||
featureBoxer.setGeneration(first); | ||
const currentValue = featureBoxer.features.value; | ||
|
||
// Assert | ||
chai.expect(currentValue).to.be.equal(first); | ||
}); | ||
|
||
mochaLoader.it("gets an second feature setting from resetting to a second generation", (): void => { | ||
// Arrange | ||
const featureBoxer = new FeatureBoxr<{ value: string }>({ | ||
generations: { | ||
[first]: { | ||
value: first | ||
}, | ||
[second]: { | ||
value: second | ||
} | ||
}, | ||
generation: first | ||
}); | ||
|
||
// Act | ||
featureBoxer.setGeneration(second); | ||
const currentValue = featureBoxer.features.value; | ||
|
||
// Assert | ||
chai.expect(currentValue).to.be.equal(second); | ||
}); |
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,79 @@ | ||
/* This file was auto-generated by gulp-shenanigans */ | ||
|
||
import { MochaLoader } from "./utils/MochaLoader"; | ||
|
||
declare const requirejs: any; | ||
declare const testDependencies: string[]; | ||
declare const testPaths: string[]; | ||
|
||
/** | ||
* Combines mocha tests into their describe() groups. | ||
*/ | ||
export const mochaLoader: MochaLoader = new MochaLoader(mocha); | ||
|
||
/** | ||
* Adds a new test under the current test path. | ||
* | ||
* @param testName The name of the test. | ||
* @param test A new test. | ||
*/ | ||
export const it: typeof mochaLoader.it = mochaLoader.it.bind(mochaLoader); | ||
|
||
/** | ||
* Adds a new test under a custom test path. | ||
* | ||
* @param path Extra path after the current test path. | ||
* @param testName The name of the test. | ||
* @param test A new test. | ||
*/ | ||
export const under: typeof mochaLoader.under = mochaLoader.under.bind(mochaLoader); | ||
|
||
/** | ||
* Informs RequireJS of the file location for a test dependency. | ||
* | ||
* @param testDependencies Modules depended upon for tests. | ||
*/ | ||
function redirectTestDependencies(dependencies: string[]): void { | ||
for (const dependency of dependencies) { | ||
requirejs.config({ | ||
paths: { | ||
[dependency.toLowerCase() + "/lib"]: `../node_modules/${dependency.toLowerCase()}/src` | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Recursively loads test paths under mocha loader. | ||
* | ||
* @param loadingPaths Test paths to load. | ||
* @param i Which index test path to load. | ||
* @param onComplete A callback for when loading is done. | ||
*/ | ||
function loadTestPaths(loadingPaths: string[], i: number, onComplete: () => void): void { | ||
"use strict"; | ||
|
||
if (i >= loadingPaths.length) { | ||
onComplete(); | ||
return; | ||
} | ||
|
||
mochaLoader.setTestPath(loadingPaths[i]); | ||
requirejs( | ||
[loadingPaths[i]], | ||
(): void => { | ||
loadTestPaths(loadingPaths, i + 1, onComplete); | ||
}); | ||
} | ||
|
||
((): void => { | ||
redirectTestDependencies(testDependencies); | ||
|
||
loadTestPaths( | ||
testPaths, | ||
0, | ||
(): void => { | ||
mochaLoader.describeTests(); | ||
mochaLoader.run(); | ||
}); | ||
})(); |
Oops, something went wrong.