Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom Types with Archetype
const Archetype = require('archetype');
// `Str` is a utility type for strings unrelated
// to archetype: https://www.npmjs.com/package/string
// It provides several convenient string utilities,
// including `.capitalize()`.
const Str = require('string');
const NameType = new Archetype({
// Archetype custom types are trivial: any function,
// including any class, is a viable `$type`.
first: { $type: Str },
last: { $type: Str }
}).compile('NameType');
const { first, last } = new NameType({ first: 'hello', last: 'world' });
// "Hello World"
console.log(first.capitalize().toString(), last.capitalize().toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment