Current section
Files
Jump to
Current section
Files
priv/build/iife/Elixir.Bootstrap.js
var Bootstrap = (function () {
'use strict';
/* @flow */
class Variable {
constructor(default_value = Symbol.for("tailored.no_value")) {
this.default_value = default_value;
}
}
class Wildcard {
constructor() {}
}
class StartsWith {
constructor(prefix) {
this.prefix = prefix;
}
}
class Capture {
constructor(value) {
this.value = value;
}
}
class HeadTail {
constructor() {}
}
class Type {
constructor(type, objPattern = {}) {
this.type = type;
this.objPattern = objPattern;
}
}
class Bound {
constructor(value) {
this.value = value;
}
}
class BitStringMatch {
constructor(...values) {
this.values = values;
}
length() {
return values.length;
}
bit_size() {
return this.byte_size() * 8;
}
byte_size() {
let s = 0;
for (let val of this.values) {
s = s + val.unit * val.size / 8;
}
return s;
}
getValue(index) {
return this.values(index);
}
getSizeOfValue(index) {
let val = this.getValue(index);
return val.unit * val.size;
}
getTypeOfValue(index) {
return this.getValue(index).type;
}
}
function variable(default_value = Symbol.for("tailored.no_value")) {
return new Variable(default_value);
}
function wildcard() {
return new Wildcard();
}
function startsWith(prefix) {
return new StartsWith(prefix);
}
function capture(value) {
return new Capture(value);
}
function headTail() {
return new HeadTail();
}
function type(type, objPattern = {}) {
return new Type(type, objPattern);
}
function bound(value) {
return new Bound(value);
}
function bitStringMatch(...values) {
return new BitStringMatch(...values);
}
/* @flow */
function is_number(value) {
return typeof value === 'number';
}
function is_string(value) {
return typeof value === 'string';
}
function is_boolean(value) {
return typeof value === 'boolean';
}
function is_symbol(value) {
return typeof value === 'symbol';
}
function is_null(value) {
return value === null;
}
function is_undefined(value) {
return typeof value === 'undefined';
}
function is_variable(value) {
return value instanceof Variable;
}
function is_wildcard(value) {
return value instanceof Wildcard;
}
function is_headTail(value) {
return value instanceof HeadTail;
}
function is_capture(value) {
return value instanceof Capture;
}
function is_type(value) {
return value instanceof Type;
}
function is_startsWith(value) {
return value instanceof StartsWith;
}
function is_bound(value) {
return value instanceof Bound;
}
function is_object(value) {
return typeof value === 'object';
}
function is_array(value) {
return Array.isArray(value);
}
function is_bitstring(value) {
return value instanceof BitStringMatch;
}
class Tuple {
constructor(...args) {
this.values = Object.freeze(args);
this.length = this.values.length;
}
get(index) {
return this.values[index];
}
count() {
return this.values.length;
}
[Symbol.iterator]() {
return this.values[Symbol.iterator]();
}
toString() {
var i,
s = "";
for (i = 0; i < this.values.length; i++) {
if (s !== "") {
s += ", ";
}
s += this.values[i].toString();
}
return "{" + s + "}";
}
put_elem(index, elem) {
if (index === this.length) {
let new_values = this.values.concat([elem]);
return new Tuple(...new_values);
}
let new_values = this.values.concat([]);
new_values.splice(index, 0, elem);
return new Tuple(...new_values);
}
remove_elem(index) {
let new_values = this.values.concat([]);
new_values.splice(index, 1);
return new Tuple(...new_values);
}
}
let process_counter = -1;
class PID {
constructor() {
process_counter = process_counter + 1;
this.id = process_counter;
}
toString() {
return "PID#<0." + this.id + ".0>";
}
}
let ref_counter = -1;
class Reference {
constructor() {
ref_counter = ref_counter + 1;
this.id = ref_counter;
this.ref = Symbol();
}
toString() {
return "Ref#<0.0.0." + this.id + ">";
}
}
class BitString$1 {
constructor(...args) {
this.value = Object.freeze(this.process(args));
this.length = this.value.length;
this.bit_size = this.length * 8;
this.byte_size = this.length;
}
get(index) {
return this.value[index];
}
count() {
return this.value.length;
}
slice(start, end = null) {
let s = this.value.slice(start, end);
let ms = s.map(elem => BitString$1.integer(elem));
return new BitString$1(...ms);
}
[Symbol.iterator]() {
return this.value[Symbol.iterator]();
}
toString() {
var i,
s = "";
for (i = 0; i < this.count(); i++) {
if (s !== "") {
s += ", ";
}
s += this.get(i).toString();
}
return "<<" + s + ">>";
}
process(bitStringParts) {
let processed_values = [];
var i;
for (i = 0; i < bitStringParts.length; i++) {
let processed_value = this['process_' + bitStringParts[i].type](bitStringParts[i]);
for (let attr of bitStringParts[i].attributes) {
processed_value = this['process_' + attr](processed_value);
}
processed_values = processed_values.concat(processed_value);
}
return processed_values;
}
process_integer(value) {
return value.value;
}
process_float(value) {
if (value.size === 64) {
return BitString$1.float64ToBytes(value.value);
} else if (value.size === 32) {
return BitString$1.float32ToBytes(value.value);
}
throw new Error('Invalid size for float');
}
process_bitstring(value) {
return value.value.value;
}
process_binary(value) {
return BitString$1.toUTF8Array(value.value);
}
process_utf8(value) {
return BitString$1.toUTF8Array(value.value);
}
process_utf16(value) {
return BitString$1.toUTF16Array(value.value);
}
process_utf32(value) {
return BitString$1.toUTF32Array(value.value);
}
process_signed(value) {
return new Uint8Array([value])[0];
}
process_unsigned(value) {
return value;
}
process_native(value) {
return value;
}
process_big(value) {
return value;
}
process_little(value) {
return value.reverse();
}
process_size(value) {
return value;
}
process_unit(value) {
return value;
}
static integer(value) {
return BitString$1.wrap(value, { 'type': 'integer', 'unit': 1, 'size': 8 });
}
static float(value) {
return BitString$1.wrap(value, { 'type': 'float', 'unit': 1, 'size': 64 });
}
static bitstring(value) {
return BitString$1.wrap(value, { 'type': 'bitstring', 'unit': 1, 'size': value.bit_size });
}
static bits(value) {
return BitString$1.bitstring(value);
}
static binary(value) {
return BitString$1.wrap(value, { 'type': 'binary', 'unit': 8, 'size': value.length });
}
static bytes(value) {
return BitString$1.binary(value);
}
static utf8(value) {
return BitString$1.wrap(value, { 'type': 'utf8', 'unit': 1, 'size': value.length });
}
static utf16(value) {
return BitString$1.wrap(value, { 'type': 'utf16', 'unit': 1, 'size': value.length * 2 });
}
static utf32(value) {
return BitString$1.wrap(value, { 'type': 'utf32', 'unit': 1, 'size': value.length * 4 });
}
static signed(value) {
return BitString$1.wrap(value, {}, 'signed');
}
static unsigned(value) {
return BitString$1.wrap(value, {}, 'unsigned');
}
static native(value) {
return BitString$1.wrap(value, {}, 'native');
}
static big(value) {
return BitString$1.wrap(value, {}, 'big');
}
static little(value) {
return BitString$1.wrap(value, {}, 'little');
}
static size(value, count) {
return BitString$1.wrap(value, { 'size': count });
}
static unit(value, count) {
return BitString$1.wrap(value, { 'unit': count });
}
static wrap(value, opt, new_attribute = null) {
let the_value = value;
if (!(value instanceof Object)) {
the_value = { 'value': value, 'attributes': [] };
}
the_value = Object.assign(the_value, opt);
if (new_attribute) {
the_value.attributes.push(new_attribute);
}
return the_value;
}
static toUTF8Array(str) {
var utf8 = [];
for (var i = 0; i < str.length; i++) {
var charcode = str.charCodeAt(i);
if (charcode < 0x80) {
utf8.push(charcode);
} else if (charcode < 0x800) {
utf8.push(0xc0 | charcode >> 6, 0x80 | charcode & 0x3f);
} else if (charcode < 0xd800 || charcode >= 0xe000) {
utf8.push(0xe0 | charcode >> 12, 0x80 | charcode >> 6 & 0x3f, 0x80 | charcode & 0x3f);
}
// surrogate pair
else {
i++;
// UTF-16 encodes 0x10000-0x10FFFF by
// subtracting 0x10000 and splitting the
// 20 bits of 0x0-0xFFFFF into two halves
charcode = 0x10000 + ((charcode & 0x3ff) << 10 | str.charCodeAt(i) & 0x3ff);
utf8.push(0xf0 | charcode >> 18, 0x80 | charcode >> 12 & 0x3f, 0x80 | charcode >> 6 & 0x3f, 0x80 | charcode & 0x3f);
}
}
return utf8;
}
static toUTF16Array(str) {
var utf16 = [];
for (var i = 0; i < str.length; i++) {
var codePoint = str.codePointAt(i);
if (codePoint <= 255) {
utf16.push(0);
utf16.push(codePoint);
} else {
utf16.push(codePoint >> 8 & 0xFF);
utf16.push(codePoint & 0xFF);
}
}
return utf16;
}
static toUTF32Array(str) {
var utf32 = [];
for (var i = 0; i < str.length; i++) {
var codePoint = str.codePointAt(i);
if (codePoint <= 255) {
utf32.push(0);
utf32.push(0);
utf32.push(0);
utf32.push(codePoint);
} else {
utf32.push(0);
utf32.push(0);
utf32.push(codePoint >> 8 & 0xFF);
utf32.push(codePoint & 0xFF);
}
}
return utf32;
}
//http://stackoverflow.com/questions/2003493/javascript-float-from-to-bits
static float32ToBytes(f) {
var bytes = [];
var buf = new ArrayBuffer(4);
new Float32Array(buf)[0] = f;
let intVersion = new Uint32Array(buf)[0];
bytes.push(intVersion >> 24 & 0xFF);
bytes.push(intVersion >> 16 & 0xFF);
bytes.push(intVersion >> 8 & 0xFF);
bytes.push(intVersion & 0xFF);
return bytes;
}
static float64ToBytes(f) {
var bytes = [];
var buf = new ArrayBuffer(8);
new Float64Array(buf)[0] = f;
var intVersion1 = new Uint32Array(buf)[0];
var intVersion2 = new Uint32Array(buf)[1];
bytes.push(intVersion2 >> 24 & 0xFF);
bytes.push(intVersion2 >> 16 & 0xFF);
bytes.push(intVersion2 >> 8 & 0xFF);
bytes.push(intVersion2 & 0xFF);
bytes.push(intVersion1 >> 24 & 0xFF);
bytes.push(intVersion1 >> 16 & 0xFF);
bytes.push(intVersion1 >> 8 & 0xFF);
bytes.push(intVersion1 & 0xFF);
return bytes;
}
}
var ErlangTypes = {
Tuple,
PID,
Reference,
BitString: BitString$1
};
/* @flow */
const BitString = ErlangTypes.BitString;
function resolveSymbol(pattern) {
return function (value) {
return is_symbol(value) && value === pattern;
};
}
function resolveString(pattern) {
return function (value) {
return is_string(value) && value === pattern;
};
}
function resolveNumber(pattern) {
return function (value) {
return is_number(value) && value === pattern;
};
}
function resolveBoolean(pattern) {
return function (value) {
return is_boolean(value) && value === pattern;
};
}
function resolveNull(pattern) {
return function (value) {
return is_null(value);
};
}
function resolveBound(pattern) {
return function (value, args) {
if (typeof value === typeof pattern.value && value === pattern.value) {
args.push(value);
return true;
}
return false;
};
}
function resolveWildcard() {
return function () {
return true;
};
}
function resolveVariable() {
return function (value, args) {
args.push(value);
return true;
};
}
function resolveHeadTail() {
return function (value, args) {
if (!is_array(value) || value.length < 2) {
return false;
}
const head = value[0];
const tail = value.slice(1);
args.push(head);
args.push(tail);
return true;
};
}
function resolveCapture(pattern) {
const matches = buildMatch(pattern.value);
return function (value, args) {
if (matches(value, args)) {
args.push(value);
return true;
}
return false;
};
}
function resolveStartsWith(pattern) {
const prefix = pattern.prefix;
return function (value, args) {
if (is_string(value) && value.startsWith(prefix)) {
args.push(value.substring(prefix.length));
return true;
}
return false;
};
}
function resolveType(pattern) {
return function (value, args) {
if (value instanceof pattern.type) {
const matches = buildMatch(pattern.objPattern);
return matches(value, args) && args.push(value) > 0;
}
return false;
};
}
function resolveArray(pattern) {
const matches = pattern.map(x => buildMatch(x));
return function (value, args) {
if (!is_array(value) || value.length != pattern.length) {
return false;
}
return value.every(function (v, i) {
return matches[i](value[i], args);
});
};
}
function resolveObject(pattern) {
let matches = {};
for (let key of Object.keys(pattern).concat(Object.getOwnPropertySymbols(pattern))) {
matches[key] = buildMatch(pattern[key]);
}
return function (value, args) {
if (!is_object(value) || pattern.length > value.length) {
return false;
}
for (let key of Object.keys(pattern).concat(Object.getOwnPropertySymbols(pattern))) {
if (!(key in value) || !matches[key](value[key], args)) {
return false;
}
}
return true;
};
}
function resolveBitString(pattern) {
let patternBitString = [];
for (let bitstringMatchPart of pattern.values) {
if (is_variable(bitstringMatchPart.value)) {
let size = getSize(bitstringMatchPart.unit, bitstringMatchPart.size);
fillArray(patternBitString, size);
} else {
patternBitString = patternBitString.concat(new BitString(bitstringMatchPart).value);
}
}
let patternValues = pattern.values;
return function (value, args) {
let bsValue = null;
if (!is_string(value) && !(value instanceof BitString)) {
return false;
}
if (is_string(value)) {
bsValue = new BitString(BitString.binary(value));
} else {
bsValue = value;
}
let beginningIndex = 0;
for (let i = 0; i < patternValues.length; i++) {
let bitstringMatchPart = patternValues[i];
if (is_variable(bitstringMatchPart.value) && bitstringMatchPart.type == 'binary' && bitstringMatchPart.size === undefined && i < patternValues.length - 1) {
throw new Error("a binary field without size is only allowed at the end of a binary pattern");
}
let size = 0;
let bsValueArrayPart = [];
let patternBitStringArrayPart = [];
size = getSize(bitstringMatchPart.unit, bitstringMatchPart.size);
if (i === patternValues.length - 1) {
bsValueArrayPart = bsValue.value.slice(beginningIndex);
patternBitStringArrayPart = patternBitString.slice(beginningIndex);
} else {
bsValueArrayPart = bsValue.value.slice(beginningIndex, beginningIndex + size);
patternBitStringArrayPart = patternBitString.slice(beginningIndex, beginningIndex + size);
}
if (is_variable(bitstringMatchPart.value)) {
switch (bitstringMatchPart.type) {
case 'integer':
if (bitstringMatchPart.attributes && bitstringMatchPart.attributes.indexOf("signed") != -1) {
args.push(new Int8Array([bsValueArrayPart[0]])[0]);
} else {
args.push(new Uint8Array([bsValueArrayPart[0]])[0]);
}
break;
case 'float':
if (size === 64) {
args.push(Float64Array.from(bsValueArrayPart)[0]);
} else if (size === 32) {
args.push(Float32Array.from(bsValueArrayPart)[0]);
} else {
return false;
}
break;
case 'bitstring':
args.push(createBitString(bsValueArrayPart));
break;
case 'binary':
args.push(String.fromCharCode.apply(null, new Uint8Array(bsValueArrayPart)));
break;
case 'utf8':
args.push(String.fromCharCode.apply(null, new Uint8Array(bsValueArrayPart)));
break;
case 'utf16':
args.push(String.fromCharCode.apply(null, new Uint16Array(bsValueArrayPart)));
break;
case 'utf32':
args.push(String.fromCharCode.apply(null, new Uint32Array(bsValueArrayPart)));
break;
default:
return false;
}
} else if (!arraysEqual(bsValueArrayPart, patternBitStringArrayPart)) {
return false;
}
beginningIndex = beginningIndex + size;
}
return true;
};
}
function getSize(unit, size) {
return unit * size / 8;
}
function arraysEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length != b.length) return false;
for (var i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}
function fillArray(arr, num) {
for (let i = 0; i < num; i++) {
arr.push(0);
}
}
function createBitString(arr) {
let integerParts = arr.map(elem => BitString.integer(elem));
return new BitString(...integerParts);
}
function resolveNoMatch() {
return function () {
return false;
};
}
/* @flow */
function buildMatch(pattern) {
if (is_variable(pattern)) {
return resolveVariable(pattern);
}
if (is_wildcard(pattern)) {
return resolveWildcard(pattern);
}
if (is_undefined(pattern)) {
return resolveWildcard(pattern);
}
if (is_headTail(pattern)) {
return resolveHeadTail(pattern);
}
if (is_startsWith(pattern)) {
return resolveStartsWith(pattern);
}
if (is_capture(pattern)) {
return resolveCapture(pattern);
}
if (is_bound(pattern)) {
return resolveBound(pattern);
}
if (is_type(pattern)) {
return resolveType(pattern);
}
if (is_array(pattern)) {
return resolveArray(pattern);
}
if (is_number(pattern)) {
return resolveNumber(pattern);
}
if (is_string(pattern)) {
return resolveString(pattern);
}
if (is_boolean(pattern)) {
return resolveBoolean(pattern);
}
if (is_symbol(pattern)) {
return resolveSymbol(pattern);
}
if (is_null(pattern)) {
return resolveNull(pattern);
}
if (is_bitstring(pattern)) {
return resolveBitString(pattern);
}
if (is_object(pattern)) {
return resolveObject(pattern);
}
return resolveNoMatch();
}
class MatchError extends Error {
constructor(arg) {
super();
if (typeof arg === "symbol") {
this.message = "No match for: " + arg.toString();
} else if (Array.isArray(arg)) {
let mappedValues = arg.map(x => x.toString());
this.message = "No match for: " + mappedValues;
} else {
this.message = "No match for: " + arg;
}
this.stack = new Error().stack;
this.name = this.constructor.name;
}
}
class Clause {
constructor(pattern, fn, guard = () => true) {
this.pattern = buildMatch(pattern);
this.arity = pattern.length;
this.optionals = getOptionalValues(pattern);
this.fn = fn;
this.guard = guard;
}
}
function clause(pattern, fn, guard = () => true) {
return new Clause(pattern, fn, guard);
}
function trampoline(fn) {
return function () {
let res = fn.apply(this, arguments);
while (res instanceof Function) {
res = res();
}
return res;
};
}
function defmatch(...clauses) {
return function (...args) {
let funcToCall = null;
let params = null;
for (let processedClause of clauses) {
let result = [];
args = fillInOptionalValues(args, processedClause.arity, processedClause.optionals);
if (processedClause.pattern(args, result) && processedClause.guard.apply(this, result)) {
funcToCall = processedClause.fn;
params = result;
break;
}
}
if (!funcToCall) {
console.error("No match for:", args);
throw new MatchError(args);
}
return funcToCall.apply(this, params);
};
}
function defmatchgen(...clauses) {
return function* (...args) {
for (let processedClause of clauses) {
let result = [];
args = fillInOptionalValues(args, processedClause.arity, processedClause.optionals);
if (processedClause.pattern(args, result) && processedClause.guard.apply(this, result)) {
return yield* processedClause.fn.apply(this, result);
}
}
console.error("No match for:", args);
throw new MatchError(args);
};
}
function getOptionalValues(pattern) {
let optionals = [];
for (let i = 0; i < pattern.length; i++) {
if (pattern[i] instanceof Variable && pattern[i].default_value != Symbol.for("tailored.no_value")) {
optionals.push([i, pattern[i].default_value]);
}
}
return optionals;
}
function fillInOptionalValues(args, arity, optionals) {
if (args.length === arity || optionals.length === 0) {
return args;
}
if (args.length + optionals.length < arity) {
return args;
}
let numberOfOptionalsToFill = arity - args.length;
let optionalsToRemove = optionals.length - numberOfOptionalsToFill;
let optionalsToUse = optionals.slice(optionalsToRemove);
for (let [index, value] of optionalsToUse) {
args.splice(index, 0, value);
if (args.length === arity) {
break;
}
}
return args;
}
function match(pattern, expr, guard = () => true) {
let result = [];
let processedPattern = buildMatch(pattern);
if (processedPattern(expr, result) && guard.apply(this, result)) {
return result;
} else {
console.error("No match for:", expr);
throw new MatchError(expr);
}
}
function match_or_default(pattern, expr, guard = () => true, default_value = null) {
let result = [];
let processedPattern = buildMatch(pattern);
if (processedPattern(expr, result) && guard.apply(this, result)) {
return result;
} else {
return default_value;
}
}
const NO_MATCH = Symbol();
function bitstring_generator(pattern, bitstring) {
return function () {
let returnResult = [];
let bsSlice = bitstring.slice(0, pattern.byte_size());
let i = 1;
while (bsSlice.byte_size == pattern.byte_size()) {
const result = match_or_default(pattern, bsSlice, () => true, NO_MATCH);
if (result != NO_MATCH) {
const [value] = result;
returnResult.push(result);
}
bsSlice = bitstring.slice(pattern.byte_size() * i, pattern.byte_size() * (i + 1));
i++;
}
return returnResult;
};
}
function list_generator(pattern, list) {
return function () {
let returnResult = [];
for (let i of list) {
const result = match_or_default(pattern, i, () => true, NO_MATCH);
if (result != NO_MATCH) {
const [value] = result;
returnResult.push(value);
}
}
return returnResult;
};
}
function list_comprehension(expression, generators) {
const generatedValues = run_generators(generators.pop()(), generators);
let result = [];
for (let value of generatedValues) {
if (expression.guard.apply(this, value)) {
result.push(expression.fn.apply(this, value));
}
}
return result;
}
function run_generators(generator, generators) {
if (generators.length == 0) {
return generator.map(x => {
if (Array.isArray(x)) {
return x;
} else {
return [x];
}
});
} else {
const list = generators.pop();
let next_gen = [];
for (let j of list()) {
for (let i of generator) {
next_gen.push([j].concat(i));
}
}
return run_generators(next_gen, generators);
}
}
function bitstring_comprehension(expression, generators) {
const generatedValues = run_generators(generators.pop()(), generators);
let result = [];
for (let value of generatedValues) {
if (expression.guard.apply(this, value)) {
result.push(expression.fn.apply(this, value));
}
}
result = result.map(x => ErlangTypes.BitString.integer(x));
return new ErlangTypes.BitString(...result);
}
var Patterns = {
defmatch,
match,
MatchError,
variable,
wildcard,
startsWith,
capture,
headTail,
type,
bound,
Clause,
clause,
bitStringMatch,
match_or_default,
defmatchgen,
list_comprehension,
list_generator,
bitstring_generator,
bitstring_comprehension,
trampoline
};
// https://github.com/airportyh/protomorphism
class Protocol {
constructor(spec) {
this.registry = new Map();
this.fallback = null;
for (const funName in spec) {
this[funName] = createFun(funName).bind(this);
}
function createFun(funName) {
return function (...args) {
const thing = args[0];
let fun = null;
if (Number.isInteger(thing) && this.hasImplementation(Core.Integer)) {
fun = this.registry.get(Core.Integer)[funName];
} else if (typeof thing === 'number' && !Number.isInteger(thing) && this.hasImplementation(Core.Float)) {
fun = this.registry.get(Core.Float)[funName];
} else if (typeof thing === 'string' && this.hasImplementation(Core.BitString)) {
fun = this.registry.get(Core.BitString)[funName];
} else if (this.hasImplementation(thing)) {
fun = this.registry.get(thing.constructor)[funName];
} else if (this.fallback) {
fun = this.fallback[funName];
}
if (fun != null) {
const retval = fun.apply(this, args);
return retval;
}
throw new Error(`No implementation found for ${thing}`);
};
}
}
implementation(type, implementation) {
if (type === null) {
this.fallback = implementation;
} else {
this.registry.set(type, implementation);
}
}
hasImplementation(thing) {
if (thing === Core.Integer || thing === Core.Float || thing === Core.BitString) {
return this.registry.has(thing);
}
return this.registry.has(thing.constructor);
}
}
function call_property(item, property) {
let prop = null;
if (typeof item === "number" || typeof item === "symbol" || typeof item === "boolean" || typeof item === "string") {
if (item[property] !== undefined) {
prop = property;
} else if (item[Symbol.for(property)] !== undefined) {
prop = Symbol.for(property);
}
} else if (property in item) {
prop = property;
} else if (Symbol.for(property) in item) {
prop = Symbol.for(property);
}
if (prop === null) {
throw new Error(`Property ${property} not found in ${item}`);
}
if (item[prop] instanceof Function) {
return item[prop]();
}
return item[prop];
}
function apply(...args) {
if (args.length === 2) {
return args[0].apply(args[0], args.slice(1));
} else {
return args[0][args[1]].apply(args[0], args.slice(2));
}
}
function contains(left, right) {
for (const x of right) {
if (Core.Patterns.match_or_default(left, x) != null) {
return true;
}
}
return false;
}
function get_global() {
if (typeof self !== "undefined") {
return self;
} else if (typeof window !== "undefined") {
return window;
} else if (typeof global !== "undefined") {
return global;
}
throw new Error("No global state found");
}
function defstruct(defaults) {
return class {
constructor(update = {}) {
const the_values = Object.assign(defaults, update);
Object.assign(this, the_values);
}
static create(updates = {}) {
const x = new this(updates);
return Object.freeze(x);
}
};
}
function defexception(defaults) {
return class extends Error {
constructor(update = {}) {
const message = update.message || "";
super(message);
const the_values = Object.assign(defaults, update);
Object.assign(this, the_values);
this.name = this.constructor.name;
this.message = message;
this[Symbol.for("__exception__")] = true;
Error.captureStackTrace(this, this.constructor.name);
}
static create(updates = {}) {
const x = new this(updates);
return Object.freeze(x);
}
};
}
function defprotocol(spec) {
return new Protocol(spec);
}
function defimpl(protocol, type, impl) {
protocol.implementation(type, impl);
}
function get_object_keys(obj) {
return Object.keys(obj).concat(Object.getOwnPropertySymbols(obj));
}
function is_valid_character(codepoint) {
try {
return String.fromCodePoint(codepoint) != null;
} catch (e) {
return false;
}
}
// https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Solution_2_%E2%80%93_rewrite_the_DOMs_atob()_and_btoa()_using_JavaScript's_TypedArrays_and_UTF-8
function b64EncodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) => String.fromCharCode(`0x${p1}`)));
}
function delete_property_from_map(map, property) {
const new_map = Object.assign(Object.create(map.constructor.prototype), map);
delete new_map[property];
return Object.freeze(new_map);
}
function class_to_obj(map) {
const new_map = Object.assign({}, map);
return Object.freeze(new_map);
}
function add_property_to_map(map, property, value) {
const new_map = Object.assign({}, map);
new_map[property] = value;
return Object.freeze(new_map);
}
function update_map(map, property, value) {
if (property in get_object_keys(map)) {
return add_property_to_map(map, property, value);
}
throw "map does not have key";
}
function bnot(expr) {
return ~expr;
}
function band(left, right) {
return left & right;
}
function bor(left, right) {
return left | right;
}
function bsl(left, right) {
return left << right;
}
function bsr(left, right) {
return left >> right;
}
function bxor(left, right) {
return left ^ right;
}
function zip(list_of_lists) {
if (list_of_lists.length === 0) {
return Object.freeze([]);
}
const new_value = [];
let smallest_length = list_of_lists[0];
for (const x of list_of_lists) {
if (x.length < smallest_length) {
smallest_length = x.length;
}
}
for (let i = 0; i < smallest_length; i++) {
const current_value = [];
for (let j = 0; j < list_of_lists.length; j++) {
current_value.push(list_of_lists[j][i]);
}
new_value.push(new Core.Tuple(...current_value));
}
return Object.freeze(new_value);
}
function can_decode64(data) {
try {
atob(data);
return true;
} catch (e) {
return false;
}
}
function remove_from_list(list, element) {
let found = false;
return list.filter(elem => {
if (!found && elem === element) {
found = true;
return false;
}
return true;
});
}
function foldl(fun, acc, list) {
let acc1 = acc;
for (const el of list) {
acc1 = fun(el, acc1);
}
return acc1;
}
function foldr(fun, acc, list) {
let acc1 = acc;
for (let i = list.length - 1; i >= 0; i--) {
acc1 = fun(list[i], acc1);
}
return acc1;
}
function keyfind(key, n, tuplelist) {
for (let i = tuplelist.length - 1; i >= 0; i--) {
if (tuplelist[i].get(n) === key) {
return tuplelist[i];
}
}
return false;
}
function keydelete(key, n, tuplelist) {
for (let i = tuplelist.length - 1; i >= 0; i--) {
if (tuplelist[i].get(n) === key) {
return tuplelist.concat([]).splice(i, 1);
}
}
return tuplelist;
}
function keystore(key, n, list, newtuple) {
for (let i = list.length - 1; i >= 0; i--) {
if (list[i].get(n) === key) {
return list.concat([]).splice(i, 1, newtuple);
}
}
return list.concat([]).push(newtuple);
}
function keymember(key, n, list) {
for (let i = list.length - 1; i >= 0; i--) {
if (list[i].get(n) === key) {
return true;
}
}
return false;
}
function keytake(key, n, list) {
if (!keymember(key, n, list)) {
return false;
}
const tuple = keyfind(key, n, list);
return new Core.Tuple(tuple.get(n), tuple, keydelete(key, n, list));
}
function keyreplace(key, n, list, newtuple) {
for (let i = list.length - 1; i >= 0; i--) {
if (list[i].get(n) === key) {
return list.concat([]).splice(i, 1, newtuple);
}
}
return list;
}
function reverse(list) {
return list.concat([]).reverse();
}
function maps_find(key, map) {
if (key in get_object_keys(map)) {
return new Core.Tuple(Symbol.for("ok"), map[key]);
}
return Symbol.for("error");
}
function flatten(list, tail = []) {
let new_list = [];
for (const e of list) {
if (Array.isArray(e)) {
new_list = new_list.concat(flatten(e));
} else {
new_list.push(e);
}
}
return Object.freeze(new_list.concat(tail));
}
function duplicate(n, elem) {
const list = [];
for (let i = 0; i < n; i++) {
list.push(elem);
}
return Object.freeze(list);
}
function mapfoldl(fun, acc, list) {
const newlist = [];
let new_acc = acc;
for (const x of list) {
const tup = fun(x, new_acc);
newlist.push(tup.get(0));
new_acc = tup.get(1);
}
return new Core.Tuple(Object.freeze(newlist), new_acc);
}
function filtermap(fun, list) {
const newlist = [];
for (const x of list) {
const result = fun(x);
if (result === true) {
newlist.push(x);
} else if (result instanceof Core.Tuple) {
newlist.push(result.get(1));
}
}
return Object.freeze(newlist);
}
function maps_fold(fun, acc, map) {
let acc1 = acc;
for (const k of get_object_keys(map)) {
acc1 = fun(k, map[k], acc1);
}
return acc1;
}
function build_namespace(ns, ns_string) {
let parts = ns_string.split(".");
let parent = ns;
if (parts[0] === "Elixir") {
parts = parts.slice(1);
}
for (const part of parts) {
if (typeof parent[part] === "undefined") {
parent[part] = {};
}
parent = parent[part];
}
return parent;
}
var Functions = {
call_property,
apply,
contains,
get_global,
defstruct,
defexception,
defprotocol,
defimpl,
get_object_keys,
is_valid_character,
b64EncodeUnicode,
delete_property_from_map,
add_property_to_map,
class_to_obj,
can_decode64,
bnot,
band,
bor,
bsl,
bsr,
bxor,
zip,
foldl,
foldr,
remove_from_list,
keydelete,
keystore,
keyfind,
keytake,
keyreplace,
reverse,
update_map,
maps_find,
flatten,
duplicate,
mapfoldl,
filtermap,
maps_fold,
build_namespace
};
function _case(condition, clauses) {
return Core.Patterns.defmatch(...clauses)(condition);
}
function cond(clauses) {
for (const clause of clauses) {
if (clause[0]) {
return clause[1]();
}
}
throw new Error();
}
function map_update(map, values) {
return Object.freeze(Object.assign(Object.create(map.constructor.prototype), map, values));
}
function _for(expression, generators, collectable_protocol, into = []) {
let [result, fun] = collectable_protocol.into(into);
const generatedValues = run_list_generators(generators.pop()(), generators);
for (const value of generatedValues) {
if (expression.guard.apply(this, value)) {
result = fun(result, new Core.Tuple(Symbol.for('cont'), expression.fn.apply(this, value)));
}
}
return fun(result, Symbol.for('done'));
}
function run_list_generators(generator, generators) {
if (generators.length == 0) {
return generator.map(x => {
if (Array.isArray(x)) {
return x;
}
return [x];
});
}
const list = generators.pop();
const next_gen = [];
for (const j of list()) {
for (const i of generator) {
next_gen.push([j].concat(i));
}
}
return run_list_generators(next_gen, generators);
}
function _try(do_fun, rescue_function, catch_fun, else_function, after_function) {
let result = null;
try {
result = do_fun();
} catch (e) {
let ex_result = null;
if (rescue_function) {
try {
ex_result = rescue_function(e);
return ex_result;
} catch (ex) {
if (ex instanceof Core.Patterns.MatchError) {
throw ex;
}
}
}
if (catch_fun) {
try {
ex_result = catch_fun(e);
return ex_result;
} catch (ex) {
if (ex instanceof Core.Patterns.MatchError) {
throw ex;
}
}
}
throw e;
} finally {
if (after_function) {
after_function();
}
}
if (else_function) {
try {
return else_function(result);
} catch (ex) {
if (ex instanceof Core.Patterns.MatchError) {
throw new Error('No Match Found in Else');
}
throw ex;
}
} else {
return result;
}
}
function _with(...args) {
let argsToPass = [];
let successFunction = null;
let elseFunction = null;
if (typeof args[args.length - 2] === 'function') {
[successFunction, elseFunction] = args.splice(-2);
} else {
successFunction = args.pop();
}
for (let i = 0; i < args.length; i++) {
const [pattern, func] = args[i];
const result = func(...argsToPass);
const patternResult = Core.Patterns.match_or_default(pattern, result);
if (patternResult == null) {
if (elseFunction) {
return elseFunction.call(null, result);
}
return result;
}
argsToPass = argsToPass.concat(patternResult);
}
return successFunction(...argsToPass);
}
var SpecialForms = {
_case,
cond,
map_update,
_for,
_try,
_with
};
const store = new Map();
const names = new Map();
function get_key(key) {
let real_key = key;
if (names.has(key)) {
real_key = names.get(key);
}
if (store.has(real_key)) {
return real_key;
}
return new Error('Key Not Found');
}
function create(key, value, name = null) {
if (name != null) {
names.set(name, key);
}
store.set(key, value);
}
function update(key, value) {
const real_key = get_key(key);
store.set(real_key, value);
}
function read(key) {
const real_key = get_key(key);
return store.get(real_key);
}
function remove(key) {
const real_key = get_key(key);
return store.delete(real_key);
}
var Store = {
create,
read,
update,
remove
};
class Integer {}
class Float {}
var Core = {
Tuple: ErlangTypes.Tuple,
PID: ErlangTypes.PID,
BitString: ErlangTypes.BitString,
Patterns,
Integer,
Float,
Functions,
SpecialForms,
Store
};
let Enum = {
all__qmark__: function (collection, fun = x => x) {
for (let elem of collection) {
if (!fun(elem)) {
return false;
}
}
return true;
},
any__qmark__: function (collection, fun = x => x) {
for (let elem of collection) {
if (fun(elem)) {
return true;
}
}
return false;
},
at: function (collection, n, the_default = null) {
if (n > this.count(collection) || n < 0) {
return the_default;
}
return collection[n];
},
concat: function (...enumables) {
return enumables[0].concat(enumables[1]);
},
count: function (collection, fun = null) {
if (fun == null) {
return collection.length;
} else {
return collection.filter(fun).length;
}
},
drop: function (collection, count) {
return collection.slice(count);
},
drop_while: function (collection, fun) {
let count = 0;
for (let elem of collection) {
if (fun(elem)) {
count = count + 1;
} else {
break;
}
}
return collection.slice(count);
},
each: function (collection, fun) {
for (let elem of collection) {
fun(elem);
}
},
empty__qmark__: function (collection) {
return collection.length === 0;
},
fetch: function (collection, n) {
if (Array.isArray(collection)) {
if (n < this.count(collection) && n >= 0) {
return new Core.Tuple(Symbol.for("ok"), collection[n]);
} else {
return Symbol.for("error");
}
}
throw new Error("collection is not an Enumerable");
},
fetch__emark__: function (collection, n) {
if (Array.isArray(collection)) {
if (n < this.count(collection) && n >= 0) {
return collection[n];
} else {
throw new Error("out of bounds error");
}
}
throw new Error("collection is not an Enumerable");
},
filter: function (collection, fun) {
let result = [];
for (let elem of collection) {
if (fun(elem)) {
result.push(elem);
}
}
return result;
},
filter_map: function (collection, filter, mapper) {
return Enum.map(Enum.filter(collection, filter), mapper);
},
find: function (collection, if_none = null, fun) {
for (let elem of collection) {
if (fun(elem)) {
return elem;
}
}
return if_none;
},
into: function (collection, list) {
return list.concat(collection);
},
map: function (collection, fun) {
let result = [];
for (let elem of collection) {
result.push(fun(elem));
}
return result;
},
map_reduce: function (collection, acc, fun) {
let mapped = Object.freeze([]);
let the_acc = acc;
for (var i = 0; i < this.count(collection); i++) {
let tuple = fun(collection[i], the_acc);
the_acc = tuple.get(1);
mapped = Object.freeze(mapped.concat([tuple.get(0)]));
}
return new Core.Tuple(mapped, the_acc);
},
member__qmark__: function (collection, value) {
return collection.includes(value);
},
reduce: function (collection, acc, fun) {
let the_acc = acc;
for (var i = 0; i < this.count(collection); i++) {
let tuple = fun(collection[i], the_acc);
the_acc = tuple.get(1);
}
return the_acc;
},
take: function (collection, count) {
return collection.slice(0, count);
},
take_every: function (collection, nth) {
let result = [];
let index = 0;
for (let elem of collection) {
if (index % nth === 0) {
result.push(elem);
}
}
return Object.freeze(result);
},
take_while: function (collection, fun) {
let count = 0;
for (let elem of collection) {
if (fun(elem)) {
count = count + 1;
} else {
break;
}
}
return collection.slice(0, count);
},
to_list: function (collection) {
return collection;
}
};
var elixir = {
Core,
Enum
};
return elixir;
}());