Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

Commit

Permalink
Initial commit (v0.1.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Goldberg committed Jun 11, 2017
0 parents commit 1cdaddf
Show file tree
Hide file tree
Showing 11 changed files with 488 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
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
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
test/**/*
npm-debug.log
8 changes: 8 additions & 0 deletions .travis.yml
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
18 changes: 18 additions & 0 deletions LICENSE.txt
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.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- {{Top}} -->
# featureboxr
[![Build Status](https://travis-ci.org/FullScreenShenanigans/featureboxr.svg?branch=master)](https://travis-ci.org/FullScreenShenanigans/featureboxr)
[![NPM version](https://badge.fury.io/js/featureboxr.svg)](http://badge.fury.io/js/featureboxr)

Gates features behind generational gaps.
<!-- {{/Top}} -->

<!-- {{Development}} -->
## Development

See [Documentation/Development](https://github.com/FullScreenShenanigans/Documentation).


<!-- {{/Development}} -->
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require("gulp-shenanigans").initialize(require("gulp"));
95 changes: 95 additions & 0 deletions src/FeatureBoxr.ts
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);
}
}
26 changes: 26 additions & 0 deletions src/IFeatureBoxr.ts
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;
}
99 changes: 99 additions & 0 deletions test/FeatureBoxr.ts
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);
});
79 changes: 79 additions & 0 deletions test/main.ts
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();
});
})();
Loading

0 comments on commit 1cdaddf

Please sign in to comment.