Skip to content
This repository has been archived by the owner on Jun 3, 2022. It is now read-only.

Commit

Permalink
Adds variable constraintType and updates dataType. (#53)
Browse files Browse the repository at this point in the history
* Adds variable constraintType and updates dataType.

* Updates constraintType property to readonly.

* Adds contraintType tests for each variable.
  • Loading branch information
chriscox committed Jan 5, 2017
1 parent fffca3c commit b4be6da
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 49 deletions.
8 changes: 6 additions & 2 deletions src/core/__tests__/BooleanVariable_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import * as sinonChai from "sinon-chai";

import { remixer } from "../Remixer";
import { BooleanVariable } from "../variables/BooleanVariable";
import { ConstraintType, DataType } from "../../lib/Constants";
import { Variable } from "../variables/Variable";
import { VariableType } from "../../lib/Constants";

const expect = chai.expect;
chai.use(sinonChai);
Expand All @@ -27,7 +27,11 @@ describe("BooleanVariable", () => {
});

it("have the correct datatype", () => {
expect(variable.dataType).to.equal(VariableType.BOOLEAN);
expect(variable.dataType).to.equal(DataType.BOOLEAN);
});

it("have the correct contraintType", () => {
expect(variable.constraintType).to.equal(ConstraintType.NONE);
});

it("have the correct title", () => {
Expand Down
8 changes: 6 additions & 2 deletions src/core/__tests__/ColorVariable_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import * as sinonChai from "sinon-chai";

import { remixer } from "../Remixer";
import { ColorVariable } from "../variables/ColorVariable";
import { ConstraintType, DataType } from "../../lib/Constants";
import { Variable } from "../variables/Variable";
import { VariableType } from "../../lib/Constants";

const expect = chai.expect;
chai.use(sinonChai);
Expand Down Expand Up @@ -33,7 +33,11 @@ describe("ColorVariable", () => {
});

it("have the correct datatype", () => {
expect(variable.dataType).to.equal(VariableType.COLOR);
expect(variable.dataType).to.equal(DataType.COLOR);
});

it("have the correct contraintType", () => {
expect(variable.constraintType).to.equal(ConstraintType.LIST);
});

it("have the correct title", () => {
Expand Down
8 changes: 6 additions & 2 deletions src/core/__tests__/NumberVariable_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as sinon from "sinon";
import * as sinonChai from "sinon-chai";

import { remixer } from "../Remixer";
import { ConstraintType, DataType } from "../../lib/Constants";
import { NumberVariable } from "../variables/NumberVariable";
import { Variable } from "../variables/Variable";
import { VariableType } from "../../lib/Constants";

const expect = chai.expect;
chai.use(sinonChai);
Expand Down Expand Up @@ -33,7 +33,11 @@ describe("NumberVariable", () => {
});

it("have the correct datatype", () => {
expect(variable.dataType).to.equal(VariableType.NUMBER);
expect(variable.dataType).to.equal(DataType.NUMBER);
});

it("have the correct contraintType", () => {
expect(variable.constraintType).to.equal(ConstraintType.LIST);
});

it("have the correct title", () => {
Expand Down
8 changes: 6 additions & 2 deletions src/core/__tests__/RangeVariable_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as sinon from "sinon";
import * as sinonChai from "sinon-chai";

import { remixer } from "../Remixer";
import { ConstraintType, DataType } from "../../lib/Constants";
import { RangeVariable } from "../variables/RangeVariable";
import { Variable } from "../variables/Variable";
import { VariableType } from "../../lib/Constants";

const expect = chai.expect;
chai.use(sinonChai);
Expand Down Expand Up @@ -37,7 +37,11 @@ describe("RangeVariable", () => {
});

it("have the correct datatype", () => {
expect(variable.dataType).to.equal(VariableType.RANGE);
expect(variable.dataType).to.equal(DataType.NUMBER);
});

it("have the correct contraintType", () => {
expect(variable.constraintType).to.equal(ConstraintType.RANGE);
});

it("have the correct title", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/core/__tests__/Remixer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as sinon from "sinon";
import * as sinonChai from "sinon-chai";

import { remixer } from "../Remixer";
import { VariableType } from "../../lib/Constants";
import { DataType } from "../../lib/Constants";

const expect = chai.expect;
chai.use(sinonChai);
Expand Down Expand Up @@ -35,7 +35,7 @@ describe("Remixer", () => {

it("should retrieve correct variable from map by key", () => {
let stringVariable = remixer.getVariable("key2");
expect(stringVariable.dataType).to.equal(VariableType.STRING);
expect(stringVariable.dataType).to.equal(DataType.STRING);
});

it("should update selected value of variable", () => {
Expand Down
8 changes: 6 additions & 2 deletions src/core/__tests__/StringVariable_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as sinon from "sinon";
import * as sinonChai from "sinon-chai";

import { remixer } from "../Remixer";
import { ConstraintType, DataType } from "../../lib/Constants";
import { StringVariable } from "../variables/StringVariable";
import { Variable } from "../variables/Variable";
import { VariableType } from "../../lib/Constants";

const expect = chai.expect;
chai.use(sinonChai);
Expand Down Expand Up @@ -33,7 +33,11 @@ describe("StringVariable", () => {
});

it("have the correct datatype", () => {
expect(variable.dataType).to.equal(VariableType.STRING);
expect(variable.dataType).to.equal(DataType.STRING);
});

it("have the correct contraintType", () => {
expect(variable.constraintType).to.equal(ConstraintType.LIST);
});

it("have the correct title", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/core/variables/BooleanVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* under the License.
*/

import { DataType } from "../../lib/Constants";
import { ISerializableData } from "../../lib/LocalStorage";
import { IVariableCallback, IVariableParams, Variable } from "./Variable";
import { VariableType } from "../../lib/Constants";

/**
* Interface for a class that represents a type of Variable for boolean values.
Expand Down Expand Up @@ -49,7 +49,7 @@ export class BooleanVariable extends Variable implements IBooleanVariableParams
defaultValue: boolean,
callback?: IVariableCallback,
) {
super(key, VariableType.BOOLEAN, defaultValue, callback);
super(key, DataType.BOOLEAN, defaultValue, callback);
}

/**
Expand Down
14 changes: 12 additions & 2 deletions src/core/variables/ColorVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* under the License.
*/

import { ConstraintType, DataType } from "../../lib/Constants";
import { ISerializableData } from "../../lib/LocalStorage";
import { IVariableCallback, IVariableListParams, Variable } from "./Variable";
import { VariableType } from "../../lib/Constants";

/**
* Interface for a class that represents a type of Variable for color values.
Expand Down Expand Up @@ -52,10 +52,20 @@ export class ColorVariable extends Variable implements IColorVariableParams {
possibleValues?: string[],
callback?: IVariableCallback,
) {
super(key, VariableType.COLOR, defaultValue, callback);
super(key, DataType.COLOR, defaultValue, callback);
this.possibleValues = possibleValues ? possibleValues : [];
}

/**
* The data constraint type for this Variable.
* @type {string}
* @readonly
*/
get constraintType(): string {
return this.possibleValues.length > 1 ?
ConstraintType.LIST : ConstraintType.NONE;
}

/**
* Clones the variable.
* @return {ColorVariable} Returns the cloned variable.
Expand Down
14 changes: 12 additions & 2 deletions src/core/variables/NumberVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* under the License.
*/

import { ConstraintType, DataType } from "../../lib/Constants";
import { ISerializableData } from "../../lib/LocalStorage";
import { IVariableCallback, IVariableListParams, Variable } from "./Variable";
import { VariableType } from "../../lib/Constants";

/**
* Interface for a class that represents a type of Variable for number values.
Expand Down Expand Up @@ -52,7 +52,7 @@ export class NumberVariable extends Variable implements INumberVariableParams {
possibleValues?: number[],
callback?: IVariableCallback,
) {
super(key, VariableType.NUMBER, defaultValue, callback);
super(key, DataType.NUMBER, defaultValue, callback);
this.possibleValues = possibleValues ? possibleValues : [];
}

Expand All @@ -71,6 +71,16 @@ export class NumberVariable extends Variable implements INumberVariableParams {
return cloned;
}

/**
* The data constraint type for this Variable.
* @type {string}
* @readonly
*/
get constraintType(): string {
return this.possibleValues.length > 1 ?
ConstraintType.LIST : ConstraintType.NONE;
}

/**
* The array of possible values for this Variable.
* @override
Expand Down
13 changes: 11 additions & 2 deletions src/core/variables/RangeVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* under the License.
*/

import { ConstraintType, DataType } from "../../lib/Constants";
import { ISerializableData } from "../../lib/LocalStorage";
import { IVariableCallback, IVariableParams, Variable } from "./Variable";
import { VariableType } from "../../lib/Constants";

/**
* Interface for a class that represents a type of Variable for a range
Expand Down Expand Up @@ -59,12 +59,21 @@ export class RangeVariable extends Variable implements IRangeVariableParams {
increment: number,
callback?: IVariableCallback,
) {
super(key, VariableType.RANGE, defaultValue, callback);
super(key, DataType.NUMBER, defaultValue, callback);
this.minValue = minValue;
this.maxValue = maxValue;
this.increment = increment;
}

/**
* The data constraint type for this Variable.
* @type {string}
* @readonly
*/
get constraintType(): string {
return ConstraintType.RANGE;
}

/**
* Clones the variable.
* @return {RangeVariable} Returns the cloned variable.
Expand Down
14 changes: 12 additions & 2 deletions src/core/variables/StringVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* under the License.
*/

import { ConstraintType, DataType } from "../../lib/Constants";
import { ISerializableData } from "../../lib/LocalStorage";
import { IVariableCallback, IVariableListParams, Variable } from "./Variable";
import { VariableType } from "../../lib/Constants";

/**
* Interface for a class that represents a type of Variable for string values.
Expand Down Expand Up @@ -52,10 +52,20 @@ export class StringVariable extends Variable implements IStringVariableParams {
possibleValues?: string[],
callback?: IVariableCallback,
) {
super(key, VariableType.STRING, defaultValue, callback);
super(key, DataType.STRING, defaultValue, callback);
this.possibleValues = possibleValues ? possibleValues : [];
}

/**
* The data constraint type for this Variable.
* @type {string}
* @readonly
*/
get constraintType(): string {
return this.possibleValues.length > 1 ?
ConstraintType.LIST : ConstraintType.NONE;
}

/**
* Clones the variable.
* @return {StringVariable} Returns the cloned variable.
Expand Down
19 changes: 18 additions & 1 deletion src/core/variables/Variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { remixer } from "../Remixer";
import { ConstraintType } from "../../lib/Constants";
import { ISerializableData } from "../../lib/LocalStorage";

/**
Expand All @@ -24,6 +25,7 @@ import { ISerializableData } from "../../lib/LocalStorage";
export interface IVariableParams {
key: string;
title: string;
constraintType: string;
dataType: string;
defaultValue: any;
selectedValue: any;
Expand Down Expand Up @@ -93,12 +95,26 @@ export class Variable implements IVariableParams {
* @return {Variable} Returns the cloned variable.
*/
clone() {
let cloned = new Variable(this.key, this.defaultValue, null);
let cloned = new Variable(
this.key,
this.dataType,
this.defaultValue,
null,
);
cloned.title = this.title;
cloned._callbacks = this._callbacks.slice();
return cloned;
}

/**
* The data constraint type for this Variable.
* @type {string}
* @readonly
*/
get constraintType(): string {
return ConstraintType.NONE;
}

/**
* The data type represented by this Variable.
* @type {string}
Expand Down Expand Up @@ -207,6 +223,7 @@ export class Variable implements IVariableParams {
serialize(): ISerializableData {
let data = <ISerializableData>{};
data.key = this.key;
data.constraintType = this.constraintType;
data.dataType = this.dataType;
data.title = this.title;
return data;
Expand Down
22 changes: 14 additions & 8 deletions src/lib/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ export const KeyEvent = {

/** Storage keys constants. */
export const StorageKey = {
REMIXER: "__remixer__",
REMIXER: "__remixer__",
};

/** Variable types constants. */
export const VariableType = {
BOOLEAN: "boolean",
COLOR: "color",
NUMBER: "number",
RANGE: "range",
STRING: "string",
/** Variable data constraints. */
export const ConstraintType = {
NONE: "__ConstraintTypeNone__",
LIST: "__ConstraintTypeList__",
RANGE: "__ConstraintTypeRange__",
};

/** Variable data types. */
export const DataType = {
BOOLEAN: "__DataTypeBoolean__",
COLOR: "__DataTypeColor__",
NUMBER: "__DataTypeNumber__",
STRING: "__DataTypeString__",
};

/** CSS class and id constants. */
Expand Down
Loading

0 comments on commit b4be6da

Please sign in to comment.