-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add structural Data Set and Data View checks (#498)
* feat: add structural Data Set and Data View checks * fix(structural-check): merge error
- Loading branch information
Showing
10 changed files
with
276 additions
and
2 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 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,39 @@ | ||
import { PartItem } from "./data-interface"; | ||
import { DataSet } from "./data-set"; | ||
|
||
/** | ||
* Check that given value is compatible with Vis Data Set interface. | ||
* | ||
* @param idProp - The expected property to contain item id. | ||
* @param v - The value to be tested. | ||
* | ||
* @returns True if all expected values and methods match, false otherwise. | ||
*/ | ||
export function isDataSetLike< | ||
Item extends PartItem<IdProp>, | ||
IdProp extends string = "id" | ||
>(idProp: IdProp, v: any): v is DataSet<Item, IdProp> { | ||
return ( | ||
typeof v === "object" && | ||
v !== null && | ||
idProp === v.idProp && | ||
typeof v.add === "function" && | ||
typeof v.clear === "function" && | ||
typeof v.distinct === "function" && | ||
typeof v.forEach === "function" && | ||
typeof v.get === "function" && | ||
typeof v.getDataSet === "function" && | ||
typeof v.getIds === "function" && | ||
typeof v.length === "number" && | ||
typeof v.map === "function" && | ||
typeof v.max === "function" && | ||
typeof v.min === "function" && | ||
typeof v.off === "function" && | ||
typeof v.on === "function" && | ||
typeof v.remove === "function" && | ||
typeof v.setOptions === "function" && | ||
typeof v.stream === "function" && | ||
typeof v.update === "function" && | ||
typeof v.updateOnly === "function" | ||
); | ||
} |
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 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 { DataView } from "./data-view"; | ||
import { PartItem } from "./data-interface"; | ||
import { isDataSetLike } from "./data-set-check"; | ||
|
||
/** | ||
* Check that given value is compatible with Vis Data View interface. | ||
* | ||
* @param idProp - The expected property to contain item id. | ||
* @param v - The value to be tested. | ||
* | ||
* @returns True if all expected values and methods match, false otherwise. | ||
*/ | ||
export function isDataViewLike< | ||
Item extends PartItem<IdProp>, | ||
IdProp extends string = "id" | ||
>(idProp: IdProp, v: any): v is DataView<Item, IdProp> { | ||
return ( | ||
typeof v === "object" && | ||
v !== null && | ||
idProp === v.idProp && | ||
typeof v.forEach === "function" && | ||
typeof v.get === "function" && | ||
typeof v.getDataSet === "function" && | ||
typeof v.getIds === "function" && | ||
typeof v.length === "number" && | ||
typeof v.map === "function" && | ||
typeof v.off === "function" && | ||
typeof v.on === "function" && | ||
typeof v.stream === "function" && | ||
isDataSetLike(idProp, v.getDataSet()) | ||
); | ||
} |
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 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 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 { expect } from "chai"; | ||
import { test, given } from "sazerac"; | ||
|
||
import { | ||
DataSet, | ||
DataView, | ||
isDataSetLike, | ||
isDataViewLike, | ||
} from "../src/entry-esnext"; | ||
|
||
function createFakeDataSet(idProp: string): any { | ||
const v = { | ||
add: (): void => {}, | ||
clear: (): void => {}, | ||
distinct: (): void => {}, | ||
forEach: (): void => {}, | ||
get: (): void => {}, | ||
getDataSet: (): any => v, | ||
getIds: (): void => {}, | ||
idProp, | ||
length: 7, | ||
map: (): void => {}, | ||
max: (): void => {}, | ||
min: (): void => {}, | ||
off: (): void => {}, | ||
on: (): void => {}, | ||
remove: (): void => {}, | ||
setOptions: (): void => {}, | ||
stream: (): void => {}, | ||
update: (): void => {}, | ||
updateOnly: (): void => {}, | ||
}; | ||
return v; | ||
} | ||
function createFakeDataView(idProp: string): any { | ||
const v = { | ||
forEach: (): void => {}, | ||
get: (): void => {}, | ||
getDataSet: (): any => createFakeDataSet(idProp), | ||
getIds: (): void => {}, | ||
idProp, | ||
length: 3, | ||
map: (): void => {}, | ||
off: (): void => {}, | ||
on: (): void => {}, | ||
stream: (): void => {}, | ||
}; | ||
return v; | ||
} | ||
|
||
describe("Type checks", function (): void { | ||
test(isDataViewLike, function (): void { | ||
given("id").expect(false); | ||
given("id", undefined).expect(false); | ||
given("id", null).expect(false); | ||
given("id", {}).expect(false); | ||
given("id", []).expect(false); | ||
|
||
given("id", new DataSet()).expect(true); | ||
given("ds", new DataSet()).expect(false); | ||
given("ds", new DataSet({ fieldId: "ds" })).expect(true); | ||
|
||
given("id", new DataView(new DataSet())).expect(true); | ||
given("dv", new DataView(new DataSet())).expect(false); | ||
given("dv", new DataView(new DataSet({ fieldId: "dv" }))).expect(true); | ||
|
||
given("id", createFakeDataSet("fds")).expect(false); | ||
given("fds", createFakeDataSet("fds")).expect(true); | ||
|
||
given("id", createFakeDataView("fdv")).expect(false); | ||
given("fdv", createFakeDataView("fdv")).expect(true); | ||
}); | ||
|
||
test(isDataSetLike, function (): void { | ||
given("id").expect(false); | ||
given("id", undefined).expect(false); | ||
given("id", null).expect(false); | ||
given("id", {}).expect(false); | ||
given("id", []).expect(false); | ||
|
||
given("id", new DataSet()).expect(true); | ||
given("ds", new DataSet()).expect(false); | ||
given("ds", new DataSet({ fieldId: "ds" })).expect(true); | ||
|
||
given("id", new DataView(new DataSet())).expect(false); | ||
given("dv", new DataView(new DataSet())).expect(false); | ||
given("dv", new DataView(new DataSet({ fieldId: "dv" }))).expect(false); | ||
|
||
given("id", createFakeDataSet("fds")).expect(false); | ||
given("fds", createFakeDataSet("fds")).expect(true); | ||
|
||
given("id", createFakeDataView("fdv")).expect(false); | ||
given("fdv", createFakeDataView("fdv")).expect(false); | ||
}); | ||
}); |