Skip to content

Instantly share code, notes, and snippets.

@vladfrangu
Created May 4, 2022 18:04
Show Gist options
  • Save vladfrangu/eb4e94ae0f3b568da47423236d0f7963 to your computer and use it in GitHub Desktop.
Save vladfrangu/eb4e94ae0f3b568da47423236d0f7963 to your computer and use it in GitHub Desktop.
import { s } from '@sapphire/shapeshift';
import _ow, { ArgumentError } from 'ow';
const ow = _ow.default;
const testObject = {
url: 'https://example.com',
userData: {
owo: true,
},
unexpected: 'behavior',
};
const shapeshiftPredicate = s.object({
url: s.string.url({ allowedProtocols: ['http:', 'https:'] }),
id: s.string.optional,
loadedUrl: s.string.url({ allowedProtocols: ['http:', 'https:'] }).optional,
uniqueKey: s.string.optional,
method: s.string.optional,
payload: s.union(s.string, s.instance(Buffer)).optional,
noRetry: s.boolean.optional,
retryCount: s.number.optional,
errorMessages: s.array(s.string).optional,
headers: s.record(s.string),
userData: s.record(s.any),
handledAt: s.union(s.date.valid, s.string.transform((str) => s.date.valid.parse(str))).optional,
keepUrlFragment: s.boolean.optional,
useExtendedUniqueKey: s.boolean.optional,
});
console.time('@sapphire/shapeshift');
try {
shapeshiftPredicate.parse(testObject);
} catch {}
console.timeEnd('@sapphire/shapeshift');
const owPredicate = ow.object.exactShape({
url: ow.string.url,
id: ow.optional.string,
loadedUrl: ow.optional.string.url,
uniqueKey: ow.optional.string,
method: ow.optional.string,
payload: ow.optional.any(ow.string, ow.buffer),
noRetry: ow.optional.boolean,
retryCount: ow.optional.number,
errorMessages: ow.optional.array.ofType(ow.string),
headers: ow.optional.object,
userData: ow.optional.object,
handledAt: ow.optional.any(ow.date, ow.string.date),
keepUrlFragment: ow.optional.boolean,
useExtendedUniqueKey: ow.optional.boolean,
});
console.time('ow exactShape');
try {
ow(testObject, 'RequestOptions', owPredicate);
} catch {}
console.timeEnd('ow exactShape');
const owSpeedUpPredicates = {
id: ow.optional.string,
loadedUrl: ow.optional.string.url,
uniqueKey: ow.optional.string,
method: ow.optional.string,
payload: ow.optional.any(ow.string, ow.buffer),
noRetry: ow.optional.boolean,
retryCount: ow.optional.number,
errorMessages: ow.optional.array.ofType(ow.string),
headers: ow.optional.object,
userData: ow.optional.object,
handledAt: ow.optional.any(ow.string.date, ow.date),
keepUrlFragment: ow.optional.boolean,
useExtendedUniqueKey: ow.optional.boolean,
};
console.time('ow speedUp');
try {
ow(testObject, 'RequestOptions', ow.object);
ow(testObject.url, 'RequestOptions.url', ow.string.url);
Object.keys(testObject).forEach((prop) => {
const predicate = owSpeedUpPredicates[prop];
const value = testObject[prop];
if (predicate) {
ow(value, `RequestOptions.${prop}`, predicate);
// 'url' is checked above because it's not optional
} else if (prop !== 'url') {
const msg = `Did not expect property \`${prop}\` to exist, got \`${value}\` in object \`RequestOptions\``;
throw new ArgumentError(msg, null);
}
});
} catch {}
console.timeEnd('ow speedUp');
const shapeshiftSpeedUpPredicates = {
id: s.string.optional,
loadedUrl: s.string.url({ allowedProtocols: ['http:', 'https:'] }).optional,
uniqueKey: s.string.optional,
method: s.string.optional,
payload: s.union(s.string, s.instance(Buffer)).optional,
noRetry: s.boolean.optional,
retryCount: s.number.optional,
errorMessages: s.array(s.string).optional,
headers: s.record(s.string),
userData: s.record(s.any),
handledAt: s.union(s.date.valid, s.string.transform((str) => s.date.valid.parse(str))).optional,
keepUrlFragment: s.boolean.optional,
useExtendedUniqueKey: s.boolean.optional,
};
const urlPredicate = s.object({
url: s.string.url({ allowedProtocols: ['http:', 'https:'] }),
});
console.time('shapeshift speedUp');
try {
urlPredicate.parse(testObject);
Object.keys(testObject).forEach((prop) => {
const predicate = shapeshiftSpeedUpPredicates[prop];
const value = testObject[prop];
if (predicate) {
// Sadly, error message won't be useful, but we're planning on improving that
predicate.parse(value);
// 'url' is checked above because it's not optional
} else if (prop !== 'url') {
const msg = `Did not expect property \`${prop}\` to exist, got \`${value}\` in object \`RequestOptions\``;
throw new Error(msg);
}
});
} catch {}
console.timeEnd('shapeshift speedUp');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment