{"version":3,"sources":["tuple.ts"],"sourcesContent":["import { ValueError } from \"./errors.ts\";\nimport { DatexObject } from \"./object.ts\";\n\n// Tuple\nexport class Tuple<T=any> {\n\n    // '#' not working with proxy?\n    _indexed:Array<T> = [];\n    _named:Map<string,T> = new Map();\n\n    constructor(initial_value?:T[]|Set<T>|Map<string,T>|Object){\n        if (initial_value instanceof Array || initial_value instanceof Set) {\n            this._indexed.push(...initial_value);\n        }\n        else if (initial_value instanceof Map) {\n            for (const [k,v] of initial_value) this._named.set(k,v);\n        }\n        else if (typeof initial_value === \"object\"){\n            for (let [name,value] of Object.entries(initial_value)) this._named.set(name, value);\n        }\n        else if (initial_value != null) throw new ValueError(\"Invalid initial value for <Tuple>\");\n    }\n\n    seal(){\n        DatexObject.seal(this);\n        return this;\n    }\n\n    get indexed(){\n        return this._indexed;\n    }\n\n    get named(){\n        return this._named;\n    }\n\n    // total size (number + string indices)\n    get size(){\n        return this._named.size + this._indexed.length;\n    }\n\n    // set value at index\n    set(index:number|bigint|string, value:any) {\n        if (typeof index === \"number\" || typeof index === \"bigint\") this._indexed[Number(index)] = value;\n        else if (typeof index === \"string\") this._named.set(index, value);\n        else throw new ValueError(\"<Tuple> key must be <text> or <integer>\")\n    }\n\n    // get value at index\n    get(index:number|bigint|string) {\n        if (typeof index === \"number\" || typeof index === \"bigint\") return this._indexed[Number(index)];\n        else if (typeof index === \"string\") return this._named.get(index);\n        else throw new ValueError(\"<Tuple> key must be <text> or <integer>\")\n    }\n\n    // get value at index\n    has(index:number|bigint|string) {\n        if (typeof index === \"number\" || typeof index === \"bigint\") return Number(index) in this._indexed;\n        else if (typeof index === \"string\") return this._named.has(index);\n        else throw new ValueError(\"<Tuple> key must be <text> or <integer>\")\n    }\n    \n    hasValue(value:any) {\n        return this._indexed.includes(value) || [...this._named.values()].includes(value)\n    }\n\n    // return copy of internal array if only number indices\n    toArray() {\n        if (this._named.size == 0) return [...this._indexed];\n        else throw new ValueError(\"<Tuple> has non-integer indices\");\n    }\n\n    // to object\n    toObject() {\n        if (this._indexed.length == 0) return Object.fromEntries(this._named);\n        else throw new ValueError(\"<Tuple> has integer indices\");\n    }\n\n    entries(): Iterable<readonly [bigint|string, T]> {\n        return this[Symbol.iterator]();\n    }\n\n    *keys(): Iterable<bigint|string> {\n        for (const entry of this._indexed.keys()) yield BigInt(entry[0]);\n        for (const entry of this._named.keys()) yield entry;\n    }\n\n    // create full copy\n    clone(){\n        const cloned = new Tuple(this.named);\n        cloned.indexed.push(...this.indexed)\n        return cloned;\n    }\n\n    // push to array\n    push(...values:any[]){\n        this._indexed.push(...values);\n    }\n\n\n    // push and add\n    spread(other:Tuple) {\n        this._indexed.push(...other.indexed);\n        for (let [name,value] of other.named.entries()) this._named.set(name, value);\n    }\n\n    *[Symbol.iterator]() {\n        for (const entry of this._indexed.entries()) {\n            yield [BigInt(entry[0]), entry[1]];\n        }\n        for (const entry of this._named.entries()) yield entry;\n    }\n\n    // generate Tuple of start..end\n    static generateRange(start:bigint|number, end:bigint|number): Tuple<bigint>{\n        if (typeof start == \"number\") start = BigInt(start);\n        if (typeof end == \"number\") end = BigInt(end);\n\n        if (typeof start != \"bigint\" || typeof end != \"bigint\") throw new ValueError(\"Range only accepts <integer> as boundaries\");\n        if (end<start) throw new ValueError(\"Second range boundary must be greater than or equal to the first boundary\");\n\n        const N = Number(end-start), range = new Tuple<bigint>();\n        let i = 0n;\n        while (i < N) range[Number(i)] = start + i++;\n\n        return range.seal();\n    }\n}"],"names":["ValueError","DatexObject","Symbol","iterator","Tuple","_indexed","_named","Map","constructor","initial_value","Array","Set","push","k","v","set","name","value","Object","entries","seal","indexed","named","size","length","index","Number","get","has","hasValue","includes","values","toArray","toObject","fromEntries","keys","entry","BigInt","clone","cloned","spread","other","generateRange","start","end","N","range","i"],"mappings":";AAAA,SAASA,UAAU,QAAQ,cAAc;AACzC,SAASC,WAAW,QAAQ,cAAc;eAyGpCC,OAAOC,QAAQ;AAtGrB,OAAO,MAAMC;IAGTC,WAAo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