Skip to content

Instantly share code, notes, and snippets.

@thoroc
Created July 21, 2023 13:28
Show Gist options
  • Save thoroc/b7cfaeecf3c726cf4abbebc6a3e23bb4 to your computer and use it in GitHub Desktop.
Save thoroc/b7cfaeecf3c726cf4abbebc6a3e23bb4 to your computer and use it in GitHub Desktop.
Demonstration of how wrangling ChatGPT for 3 hours gave code that compile ....
import * as util from 'util';
const url = 'https://bitbucket.org/example-org/example-repo';
let address = new URL(url);
console.log(address);
class BitbucketRepo extends URL {
public readonly organisation: string;
public readonly name: string;
constructor(url: string) {
super(url);
const urlParts = this.pathname.split('/');
this.organisation = urlParts[1];
this.name = urlParts[2];
}
// Override the util.inspect.custom method to display custom object representation
[util.inspect.custom](_: number, options: util.InspectOptionsStylized): string {
const allProperties: { [key: string]: any } = {};
// Get property values from URL.prototype
Object.getOwnPropertyNames(URL.prototype).forEach((key) => {
allProperties[key] = (this as any)[key];
});
// Get property values from BitbucketRepo instance
Reflect.ownKeys(this).forEach((key) => {
if (typeof key === 'string') {
allProperties[key] = (this as any)[key];
}
});
// Keys to exclude from the output
const excludeKeys = ['constructor', 'inspect', 'toString', 'toJSON'];
// Format all properties
const innerInspect = Object.entries(allProperties)
.map(([key, value]) => {
const depth = (options.depth !== undefined && options.depth !== null) ? options.depth - 1 : options.depth;
if (excludeKeys.includes(key)) return '';
const formattedValue = util.inspect(value, {
...options,
depth,
});
return `${key}: ${formattedValue}`;
})
.filter(Boolean)
.join(',\n ');
const className = this.constructor.name;
return `${className} {\n ${innerInspect}\n}`;
}
}
const repo = new BitbucketRepo(url);
console.log(repo);
// console.dir(repo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment