-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sandbox.cement): fix recursive cementing
Cementing wasn't applied recursively due to a combination of bad detection and bad recursion
- Loading branch information
Jake Lauer
authored and
Jake Lauer
committed
Jun 30, 2024
1 parent
2b37b6e
commit d155739
Showing
11 changed files
with
230 additions
and
38 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
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
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
111 changes: 111 additions & 0 deletions
111
packages/sandbox/lib/sandbox/_test/sandbox-transform.test.ts
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,111 @@ | ||
import { expect } from "chai"; | ||
import sinon from "sinon"; | ||
import { sandboxTransform } from "../sandbox-transform"; | ||
|
||
describe("deepSandboxTransform", function() | ||
{ | ||
interface TestObject { | ||
[key: string]: any; | ||
} | ||
|
||
it("should apply transform to values that match the predicate", function() | ||
{ | ||
const obj: TestObject = { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
}; | ||
const transform = sinon.spy((val: any) => val * 2); | ||
const predicate = (val: any) => val > 1; | ||
|
||
const result = sandboxTransform(obj, transform, predicate); | ||
|
||
expect(result).to.deep.equal({ | ||
a: 1, | ||
b: 4, | ||
c: 6, | ||
}); | ||
expect(transform.calledTwice).to.be.true; | ||
expect(transform.firstCall.calledWith(2)).to.be.true; | ||
expect(transform.secondCall.calledWith(3)).to.be.true; | ||
}); | ||
|
||
it("should not apply transform to values that do not match the predicate", function() | ||
{ | ||
const obj: TestObject = { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
}; | ||
const transform = sinon.spy((val: any) => val * 2); | ||
const predicate = (val: any) => val > 3; | ||
|
||
const result = sandboxTransform(obj, transform, predicate); | ||
|
||
expect(result).to.deep.equal({ | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
}); | ||
expect(transform.notCalled).to.be.true; | ||
}); | ||
|
||
it("should apply transform to all values if no predicate is provided", function() | ||
{ | ||
const obj: TestObject = { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
}; | ||
const transform = sinon.spy((val: any) => val * 2); | ||
|
||
const result = sandboxTransform(obj, transform); | ||
|
||
expect(result).to.deep.equal({ | ||
a: 2, | ||
b: 4, | ||
c: 6, | ||
}); | ||
expect(transform.calledThrice).to.be.true; | ||
expect(transform.firstCall.calledWith(1)).to.be.true; | ||
expect(transform.secondCall.calledWith(2)).to.be.true; | ||
expect(transform.thirdCall.calledWith(3)).to.be.true; | ||
}); | ||
|
||
it("should not handle nested objects", function() | ||
{ | ||
const obj: TestObject = { | ||
a: { | ||
d: 1, | ||
}, | ||
b: 2, | ||
c: 3, | ||
}; | ||
const transform = sinon.spy((val: any) => (typeof val === "number" ? val * 2 : val)); | ||
const predicate = (val: any) => typeof val === "number"; | ||
|
||
const result = sandboxTransform(obj, transform, predicate); | ||
|
||
expect(result).to.deep.equal({ | ||
a: { | ||
d: 1, | ||
}, | ||
b: 4, | ||
c: 6, | ||
}); | ||
expect(transform.calledTwice).to.be.true; | ||
expect(transform.firstCall.calledWith(2)).to.be.true; | ||
expect(transform.secondCall.calledWith(3)).to.be.true; | ||
}); | ||
|
||
it("should handle empty objects", function() | ||
{ | ||
const obj: TestObject = {}; | ||
const transform = sinon.spy((val: any) => val * 2); | ||
|
||
const result = sandboxTransform(obj, transform); | ||
|
||
expect(result).to.deep.equal({}); | ||
expect(transform.notCalled).to.be.true; | ||
}); | ||
}); |
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,28 @@ | ||
export function sandboxTransform< | ||
T extends object, | ||
>(obj: T, transform: (obj: any) => any, predicate?: (val: any) => boolean): T | ||
{ | ||
const transformValue = (val: any): any => | ||
{ | ||
if (!predicate || predicate?.(val)) | ||
{ | ||
return transform(val); | ||
} | ||
else | ||
{ | ||
return val; | ||
} | ||
}; | ||
|
||
for (const key in obj) | ||
{ | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) | ||
{ | ||
const val = obj[key]; | ||
const transformed = transformValue(val); | ||
obj[key] = transformed; | ||
} | ||
} | ||
|
||
return obj; | ||
} |
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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import isValidObject from "./is-valid-object"; | ||
import type { AtLeastOneItemOfType } from "./types"; | ||
|
||
export default function isElligibleForSandbox<T>(value: any): value is T | ||
type ElligibleForSandbox = object | AtLeastOneItemOfType<object>; | ||
|
||
export default function isElligibleForSandbox(value: any): value is ElligibleForSandbox | ||
{ | ||
return Array.isArray(value) || isValidObject(value); | ||
} |
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,5 @@ | ||
export type AtLeastOnePropertyOfType<T, K extends keyof T, V> = { | ||
[P in K]: T[P] extends V ? P : never; | ||
}[K]; | ||
|
||
export type AtLeastOneItemOfType<T> = [T, ...any[]] | [...any[], T]; |