Skip to content

Commit

Permalink
Fix for IE10
Browse files Browse the repository at this point in the history
  • Loading branch information
metafloor committed Jun 16, 2016
1 parent 6554bf8 commit ca4147f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ See the [Version 1.0 Release Announcement](https://github.com/metafloor/bwip-js/
## Status

* Current bwip-js version is 1.0.3 (2016-06-16)
* Current bwip-js version is 1.0.4 (2016-06-16)
* Current BWIPP version is 2016-06-03
* Node.js compatibility >= v0.10
* npm dependencies: none
Expand Down
2 changes: 1 addition & 1 deletion bwipp-min.js

Large diffs are not rendered by default.

97 changes: 51 additions & 46 deletions bwipp.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ function BWIPP() {
$j = i;
} else if (!(a instanceof Array)) {
a = new Array(+arguments[0]);
a.fill(null);
for (var i = 0, l = a.length; i < l; i++) {
a[i] = null;
}
}
a.b = a; // base array
a.o = 0; // offset into base
Expand All @@ -62,7 +64,7 @@ function BWIPP() {
d['\uffff' + k] = $k[i];
} else if (t === 'string') {
d[k] = $k[i];
} else if (ArrayBuffer.isView(k)) {
} else if (k instanceof Uint8Array) {
d[$z(k)] = $k[i];
} else {
throw 'dict-not-a-valid-key(' + k + ')';
Expand Down Expand Up @@ -99,7 +101,7 @@ function BWIPP() {
// Primarily designed to convert uint8-string to string, but will call the
// the toString() method on any value.
function $z(s) {
if (ArrayBuffer.isView(s)) {
if (s instanceof Uint8Array) {
// Postscript treats nul-char as end of string, even if string is
// longer.
for (var i = 0, l = s.length; i < l && s[i]; i++);
Expand All @@ -116,7 +118,7 @@ function BWIPP() {
if (typeof dst === 'string') {
dst = $s(dst);
}
if (ArrayBuffer.isView(src)) {
if (src instanceof Uint8Array) {
for (var i = 0, l = src.length; i < l; i++) {
dst[i] = src[i];
}
Expand Down Expand Up @@ -155,13 +157,13 @@ function BWIPP() {
// s : source
// k : key
function $get(s, k) {
if (ArrayBuffer.isView(s)) {
if (s instanceof Uint8Array) {
return s[k];
}
if (typeof s === 'string') {
return s.charCodeAt(k);
}
if (Array.isArray(s)) {
if (s instanceof Array) {
return s.b[s.o + k];
}
// Must be a dict object : with postscript dict objects, a number key
Expand All @@ -170,7 +172,7 @@ function BWIPP() {
if (typeof k === 'number') {
return s['\uffff' + k];
}
if (ArrayBuffer.isView(k)) {
if (k instanceof Uint8Array) {
return s[$z(k)];
}
return s[k];
Expand All @@ -181,12 +183,12 @@ function BWIPP() {
// k : key
// v : value
function $put(d, k, v) {
if (ArrayBuffer.isView(d)) {
if (d instanceof Uint8Array) {
d[k] = v;
} else if (Array.isArray(d)) {
} else if (d instanceof Array) {
d.b[d.o + k] = v;
} else if (typeof d == 'object') {
if (ArrayBuffer.isView(k)) {
if (k instanceof Uint8Array) {
d[$z(k)] = v;
} else {
d[typeof k == 'number' ? '\uffff' + k : k] = v;
Expand All @@ -201,10 +203,10 @@ function BWIPP() {
// o : offset
// l : length
function $geti(s, o, l) {
if (ArrayBuffer.isView(s)) {
if (s instanceof Uint8Array) {
return s.subarray(o, o + l);
}
if (Array.isArray(s)) {
if (s instanceof Array) {
var a = new Array(l);
a.b = s.b; // base array
a.o = s.o + o; // offset into base
Expand All @@ -219,7 +221,7 @@ function BWIPP() {
// o : offset
// s : src
function $puti(d, o, s) {
if (ArrayBuffer.isView(d)) {
if (d instanceof Uint8Array) {
if (typeof s == 'string') {
for (var i = 0, l = s.length; i < l; i++) {
d[o + i] = s.charCodeAt(i);
Expand All @@ -231,7 +233,7 @@ function BWIPP() {
d[o + i] = s[i];
}
}
} else if (Array.isArray(d)) {
} else if (d instanceof Array) {
// Operate on the base arrays
var darr = d.b;
var doff = o + d.o;
Expand Down Expand Up @@ -259,13 +261,13 @@ function BWIPP() {
if (t == 'boolean') {
return 'booleantype';
}
if (t == 'string' || ArrayBuffer.isView(v)) {
if (t == 'string' || v instanceof Uint8Array) {
return 'stringtype';
}
if (t == 'function') {
return 'operatortype';
}
if (Array.isArray(v)) {
if (v instanceof Array) {
return 'arraytype';
}
return 'dicttype';
Expand All @@ -281,16 +283,18 @@ function BWIPP() {
// string seek search suffix match prefix true %if-found
// string false %if-not-found
function $search(str, seek) {
if (!ArrayBuffer.isView(str)) {
if (!(str instanceof Uint8Array)) {
str = $s(str);
}
var ls = str.length;

// Virtually all uses of search in BWIPP are for single-characters.
// Optimize for that case.
if (seek.length == 1) {
var cd = ArrayBuffer.isView(seek) ? seek[0] : seek.charCodeAt(0);
var i = str.indexOf(cd);
if (i >= 0) {
$k[$j++] = str.subarray(i + 1, l);
var cd = seek instanceof Uint8Array ? seek[0] : seek.charCodeAt(0);
for (var i = 0; i < ls && str[i] != cd; i++);
if (i < ls) {
$k[$j++] = str.subarray(i + 1);
$k[$j++] = str.subarray(i, i + 1);
$k[$j++] = str.subarray(0, i);
$k[$j++] = true;
Expand All @@ -302,21 +306,22 @@ function BWIPP() {
}

// Slow path,
if (!ArrayBuffer.isView(seek)) {
if (!(seek instanceof Uint8Array)) {
seek = $(seek);
}
var i = str.indexOf(seek[0]);
var l = seek.length;
while (i >= 0) {
for (var j = 1; j < l && str[i + j] === seek[j]; j++);
if (j === l) {
$k[$j++] = str.subarray(i + l);
$k[$j++] = str.subarray(i, i + l);
var lk = seek.length;
var cd = seek[0];
for (var i = 0; i < ls && str[i] != cd; i++);
while (i < ls) {
for (var j = 1; j < lk && str[i + j] === seek[j]; j++);
if (j === lk) {
$k[$j++] = str.subarray(i + lk);
$k[$j++] = str.subarray(i, i + lk);
$k[$j++] = str.subarray(0, i);
$k[$j++] = true;
return;
}
i = str.indexOf(seek[0], i + 1);
for (i++; i < ls && str[i] != cd; i++);
}
$k[$j++] = str;
$k[$j++] = false;
Expand All @@ -325,12 +330,12 @@ function BWIPP() {
// The callback is omitted when forall is being used just to push onto the
// stack.
function $forall(o, cb) {
if (ArrayBuffer.isView(o)) {
if (o instanceof Uint8Array) {
for (var i = 0, l = o.length; i < l; i++) {
$k[$j++] = o[i];
cb && cb();
}
} else if (Array.isArray(o)) {
} else if (o instanceof Array) {
// The array may be a view.
for (var a = o.b, i = o.o, l = o.o + o.length; i < l; i++) {
$k[$j++] = a[i];
Expand Down Expand Up @@ -378,7 +383,7 @@ function BWIPP() {
if (typeof a === 'string' && typeof b === 'string') {
return a == b;
}
if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
if (a instanceof Uint8Array && b instanceof Uint8Array) {
if (a.length != b.length) {
return false;
}
Expand All @@ -389,9 +394,9 @@ function BWIPP() {
}
return true;
}
if (ArrayBuffer.isView(a) && typeof b === 'string' ||
ArrayBuffer.isView(b) && typeof a === 'string') {
if (ArrayBuffer.isView(a)) {
if (a instanceof Uint8Array && typeof b === 'string' ||
b instanceof Uint8Array && typeof a === 'string') {
if (a instanceof Uint8Array) {
a = $z(a);
} else {
b = $z(b);
Expand All @@ -406,40 +411,40 @@ function BWIPP() {
}

function $lt(a, b) {
if (ArrayBuffer.isView(a)) {
if (a instanceof Uint8Array) {
a = $z(a);
}
if (ArrayBuffer.isView(b)) {
if (b instanceof Uint8Array) {
b = $z(b);
}
return a < b;
}

function $le(a, b) {
if (ArrayBuffer.isView(a)) {
if (a instanceof Uint8Array) {
a = $z(a);
}
if (ArrayBuffer.isView(b)) {
if (b instanceof Uint8Array) {
b = $z(b);
}
return a <= b;
}

function $gt(a, b) {
if (ArrayBuffer.isView(a)) {
if (a instanceof Uint8Array) {
a = $z(a);
}
if (ArrayBuffer.isView(b)) {
if (b instanceof Uint8Array) {
b = $z(b);
}
return a > b;
}

function $ge(a, b) {
if (ArrayBuffer.isView(a)) {
if (a instanceof Uint8Array) {
a = $z(a);
}
if (ArrayBuffer.isView(b)) {
if (b instanceof Uint8Array) {
b = $z(b);
}
return a >= b;
Expand Down Expand Up @@ -471,13 +476,13 @@ function BWIPP() {
return 'null';
} else if (v === undefined) {
return '<undefined>';
} else if (Array.isArray(v)) {
} else if (v instanceof Array) {
var s = '<array,' + v.o + ',' + v.length + '>[';
for (var j = v.o, a = v.b, l = v.length + v.o; j < l; j++) {
s += (j == v.o ? '' : ',') + tostring(a[j]);
}
return s + ']';
} else if (ArrayBuffer.isView(v)) {
} else if (v instanceof Uint8Array) {
return '(' + $z[v] + ')';
} else if (typeof v === 'object') {
var s = '<<';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bwip-js",
"version": "1.0.3",
"version": "1.0.4",
"description": "Barcode generator supporting over 90 types and standards.",
"main": "node-bwipjs",
"scripts": {
Expand Down

0 comments on commit ca4147f

Please sign in to comment.