Skip to content

Instantly share code, notes, and snippets.

@burtonator
Last active July 8, 2021 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save burtonator/e52dc9a7b1e4f339958bd36b760d3b65 to your computer and use it in GitHub Desktop.
Save burtonator/e52dc9a7b1e4f339958bd36b760d3b65 to your computer and use it in GitHub Desktop.
import {assert} from 'chai';
export namespace Dictionaries {
/**
* Create a deep copy of a dictionary such that all of the origina keys are maintained
* and copied into a new dictionary.
*
* This is used when we have to create a copy of a dictionary to prevent concurrent mutation
* or when we need to copy it and then make changes to the new dictionary.
*
* The values in the map could be arrays, other dictionaries, sets, maps, strings, arrays, etc.
*
* Make sure to handle all cases.
*
* This needs to be fully recursive including dictionaries contain other dictionaries
* and arrays.
*/
export function deepCopy(dict: {[key: string]: any}) {
// TODO: implement this function from the above function definition.
}
}
describe("Dictionaries", function() {
// TODO: make sure all of the following tests pass with your new code.
it("basic tests", function() {
const dict: any = {
"hello: "world"
};
const copy = Dictionaries.deepCopy(dict);
assert.deepEqual(dict, copy);
});
it("basic integrity", function() {
const dict: any = {
"hello: "world"
};
const copy = Dictionaries.deepCopy(dict);
dict['foo'] = 'bar';
assert.deepEqual(copy, {
"hello: "world"
});
});
it("inner integrity", function() {
const dict: any = {
"hello: "world",
"inner": {
"foo": "bar"
}
};
const copy = Dictionaries.deepCopy(dict);
dict['inner']['foo'] = 'cat';
assert.deepEqual(copy, {
"hello: "world",
"inner": {
"foo": "bar"
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment