Skip to content
This repository has been archived by the owner on Sep 16, 2020. It is now read-only.

Commit

Permalink
using Map instead
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed Sep 10, 2020
1 parent c966467 commit 37398b5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
19 changes: 11 additions & 8 deletions index.js
Expand Up @@ -16,7 +16,7 @@
/* istanbul ignore next */
const G = typeof(self) === 'object' ? self : global;

const casts = create(null);
const casts = new Map;
const types = [];

// basic primitives, casted via constructors
Expand All @@ -32,7 +32,7 @@
const [type] = keys(definition);
const Class = G[definition[type]];
const cast = self => typeof(self) === type ? self : Class(self);
casts[type] = cast;
casts.set(type, cast);
defineProperty(Array.prototype, type, {get: map(cast)});
defineProperty(Object.prototype, type, {get: fn(cast)});
types.push(definition);
Expand All @@ -53,7 +53,7 @@
return self;
throw new TypeError(String(self) + ' is not a ' + name);
};
casts[type] = cast;
casts.set(type, cast);
defineProperty(Array.prototype, type, {get: map(cast)});
defineProperty(Object.prototype, type, {get: fn(cast)});
types.push(definition);
Expand All @@ -63,6 +63,9 @@
// Rust like primitives (plus uc8) casted via value assignment
// const {f32: coords} = [lat, long];
[
{f: 'Float32Array'},
{i: 'Int32Array'},
{u: 'Uint32Array'},
{f32: 'Float32Array'},
{f64: 'Float64Array'},
{i8: 'Int8Array'},
Expand All @@ -81,7 +84,7 @@
if (typeof(Class) === 'function') {
const reference = new Class(1);
const cast = self => ((reference[0] = self), reference[0]);
casts[type] = cast;
casts.set(type, cast);
defineProperty(Array.prototype, type, {
get() { return new Class(this); }
});
Expand All @@ -100,7 +103,7 @@
const Class = definition[type];
const cast = G[Class.slice(0, Class.indexOf('64'))];
if (typeof(cast) === 'function' && typeof(G[Class]) === 'function') {
casts[type] = cast;
casts.set(type, cast);
defineProperty(Array.prototype, type, {get: map(cast)});
defineProperty(Object.prototype, type, {get: fn(cast)});
types.push(definition);
Expand All @@ -116,7 +119,7 @@
const [type] = keys(definition);
const Class = G[definition[type]];
const cast = self => Class.from(self);
casts[type] = cast;
casts.set(type, cast);
defineProperty(Array.prototype, type, {get() { return cast(this); }});
defineProperty(Object.prototype, type, {get: fn(cast)});
types.push(definition);
Expand Down Expand Up @@ -210,9 +213,9 @@
}

function getCast(type) {
if (!(type in casts))
if (!casts.has(type))
throw new TypeError('unknown type ' + type);
return casts[type];
return casts.get(type);
}

function map(cast) {
Expand Down
2 changes: 1 addition & 1 deletion min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 37398b5

Please sign in to comment.