<commit_before>import {extendModel, initModel, Model} from "./model"
import {cast, checkAssertions, checkDefinition, extendDefinition} from "./definition"
import {extend, isFunction, setConstructor, toString} from "./helpers"

const SET_MUTATORS = ["add", "delete", "clear"]

export default function SetModel() {

	const model = function (iterable) {
		const set = new Set(iterable)
		if (!model.validate(set)) return
		return new Proxy(set, {
			getPrototypeOf: () => model.prototype,

			get(set, key) {
				let val = set[key]
				if (!isFunction(val)) return val;

				return new Proxy(val, {
					apply: (fn, ctx, args) => {
						if (key === "add") {
							args[0] = cast(args[0], model.definition)
						}

						if (SET_MUTATORS.includes(key)) {
							const testSet = new Set(set)
							fn.apply(testSet, args)
							model.validate(testSet)<commit_msg>add default and autocast to set models<commit_after>import {extendModel, initModel, Model} from "./model"
import {cast, checkAssertions, checkDefinition, extendDefinition} from "./definition"
import {extend, isFunction, setConstructor, toString} from "./helpers"

const SET_MUTATORS = ["add", "delete", "clear"]

export default function SetModel() {

	const model = function (iterable = model.default) {
		const castValue = val => cast(val, model.definition)
		const set       = new Set([...iterable].map(castValue))

		if (!model.validate(set)) return

		return new Proxy(set, {
			getPrototypeOf: () => model.prototype,

			get(set, key) {
				let val = set[key]
				if (!isFunction(val)) return val;

				return new Proxy(val, {
					apply: (fn, ctx, args) => {
						if (key === "add") {
							args[0] = castValue(args[0])
						}

						if (SET_MUTATORS.includes(key)) {
							const testSet = new Set(set)
							fn.apply(testSet, args)
							model.validate(testSet)
{'fraction_non_alphanumeric': 0.1431682090364725, 'fraction_numerical': 0.002177463255307567, 'mean_word_length': 3.5270935960591134, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6912626', 'n_tokens_mistral': 704, 'n_tokens_neox': 580, 'n_words': 162}