Skip to content

Commit

Permalink
add a JSONTable to datastore, and fix the deepCopy function to return…
Browse files Browse the repository at this point in the history
… the generic type it was given
  • Loading branch information
sccolbert committed Jun 28, 2017
1 parent efff43b commit 95bd550
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 39 deletions.
14 changes: 3 additions & 11 deletions packages/coreutils/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,19 @@ namespace JSONExt {
* @returns A deep copy of the given JSON value.
*/
export
function deepCopy(value: JSONPrimitive): JSONPrimitive;
export
function deepCopy(value: JSONArray): JSONArray;
export
function deepCopy(value: JSONObject): JSONObject;
export
function deepCopy(value: JSONValue): JSONValue;
export
function deepCopy(value: JSONValue): JSONValue {
function deepCopy<T extends JSONValue>(value: T): T {
// Do nothing for primitive values.
if (isPrimitive(value)) {
return value;
}

// Deep copy an array.
if (isArray(value)) {
return deepArrayCopy(value);
return deepArrayCopy(value) as T;
}

// Deep copy an object.
return deepObjectCopy(value);
return deepObjectCopy(value as JSONObject) as T;
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/datastore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"lib": "lib/"
},
"dependencies": {
"@phosphor/coreutils": "^1.1.2",
"@phosphor/signaling": "^1.2.2"
},
"devDependencies": {
Expand Down
58 changes: 30 additions & 28 deletions packages/datastore/src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
import {
JSONPrimitive, JSONValue
} from '@phosphor/coreutils';


/**
Expand All @@ -21,40 +24,39 @@
export
namespace Table {
/**
* A type alias for a scalar table value.
* A type alias for a primitive list.
*/
export
type Value = string | number | boolean | null;
type List<T extends JSONPrimitive> = ReadonlyArray<T>;

/**
* A type alias for a list table value.
* A type alias for a primitive record.
*/
export
type List<T extends Value> = ReadonlyArray<T>;
type Record<K extends string> = { readonly [P in K]: JSONPrimitive; };

/**
* A type alias for a record table value.
* A type alias for a table which holds primitive lists.
*/
export
type Record<K extends string> = { readonly [P in K]: Value; };
type ListTable<T extends JSONPrimitive> = { readonly [id: string]: List<T>; };

/**
* A type alias for a table which holds scalar values.
* A type alias for a table which holds primitive records.
*/
export
type ValueTable<T extends Value> = { readonly [id: string]: T; };

/**
* A type alias for a table which holds lists.
*/
export
type ListTable<T extends Value> = { readonly [id: string]: List<T>; };
type RecordTable<T extends Record<keyof T>> = { readonly [id: string]: T; };

/**
* A type alias for a table which holds records.
* A type alias for a table which holds arbitrary JSON.
*
* #### Notes
* Since JSON can be arbitrarily nested, the user is responsible for
* making immutable updates to the JSON, and calling `Table.replace`
* to update the table with the new value.
*/
export
type RecordTable<T extends Record<keyof T>> = { readonly [id: string]: T; };
type JSONTable<T extends JSONValue> = { readonly [id: string]: T; };

/**
* Insert a new entry into a table.
Expand All @@ -70,11 +72,11 @@ namespace Table {
* @throws An error if the id already exists in the table.
*/
export
function insert<T extends Record<keyof T>>(table: RecordTable<T>, id: string, entry: T): RecordTable<T>;
function insert<T extends JSONValue>(table: JSONTable<T>, id: string, entry: T): JSONTable<T>;
export
function insert<T extends Value>(table: ListTable<T>, id: string, entry: T): ListTable<T>;
function insert<T extends Record<keyof T>>(table: RecordTable<T>, id: string, entry: T): RecordTable<T>;
export
function insert<T extends Value>(table: ValueTable<T>, id: string, entry: T): ValueTable<T>;
function insert<T extends JSONPrimitive>(table: ListTable<T>, id: string, entry: T): ListTable<T>;
export
function insert(table: any, id: string, entry: any): any {
// Throw an error if the id already exists.
Expand All @@ -100,11 +102,11 @@ namespace Table {
* @throws An error if the id does not exist in the table.
*/
export
function replace<T extends Record<keyof T>>(table: RecordTable<T>, id: string, entry: T): RecordTable<T>;
function replace<T extends JSONValue>(table: JSONTable<T>, id: string, entry: T): JSONTable<T>;
export
function replace<T extends Value>(table: ListTable<T>, id: string, entry: T): ListTable<T>;
function replace<T extends Record<keyof T>>(table: RecordTable<T>, id: string, entry: T): RecordTable<T>;
export
function replace<T extends Value>(table: ValueTable<T>, id: string, entry: T): ValueTable<T>;
function replace<T extends JSONPrimitive>(table: ListTable<T>, id: string, entry: T): ListTable<T>;
export
function replace(table: any, id: string, entry: any): any {
// Throw an error if the id does not exist.
Expand Down Expand Up @@ -133,11 +135,11 @@ namespace Table {
* @throws An error if the id does not exist in the table.
*/
export
function remove<T extends Record<keyof T>>(table: RecordTable<T>, id: string): RecordTable<T>;
function remove<T extends JSONValue>(table: JSONTable<T>, id: string): JSONTable<T>;
export
function remove<T extends Value>(table: ListTable<T>, id: string): ListTable<T>;
function remove<T extends Record<keyof T>>(table: RecordTable<T>, id: string): RecordTable<T>;
export
function remove<T extends Value>(table: ValueTable<T>, id: string): ValueTable<T>;
function remove<T extends JSONPrimitive>(table: ListTable<T>, id: string): ListTable<T>;
export
function remove(table: any, id: string): any {
// Throw an error if the id does not exist.
Expand Down Expand Up @@ -197,7 +199,7 @@ namespace Table {
* @throws An error if the id does not exist in the table.
*/
export
function append<T extends Value>(table: ListTable<T>, id: string, values: T[]): ListTable<T> {
function append<T extends JSONPrimitive>(table: ListTable<T>, id: string, values: T[]): ListTable<T> {
// Throw an error if the id does not exist.
if (!(id in table)) {
throw new Error(`Id '${id}' does not exist in the table.`);
Expand All @@ -224,7 +226,7 @@ namespace Table {
* @throws An error if the id does not exist in the table.
*/
export
function prepend<T extends Value>(table: ListTable<T>, id: string, values: T[]): ListTable<T> {
function prepend<T extends JSONPrimitive>(table: ListTable<T>, id: string, values: T[]): ListTable<T> {
// Throw an error if the id does not exist.
if (!(id in table)) {
throw new Error(`Id '${id}' does not exist in the table.`);
Expand Down Expand Up @@ -255,7 +257,7 @@ namespace Table {
* @throws An error if the id does not exist in the table.
*/
export
function splice<T extends Value>(table: ListTable<T>, id: string, index: number, count: number, values: T[]): ListTable<T> {
function splice<T extends JSONPrimitive>(table: ListTable<T>, id: string, index: number, count: number, values: T[]): ListTable<T> {
// Throw an error if the id does not exist.
if (!(id in table)) {
throw new Error(`Id '${id}' does not exist in the table.`);
Expand Down

0 comments on commit 95bd550

Please sign in to comment.