Skip to content

Commit 6ed44a5

Browse files
committed
Added association support (by filename and mime-type).
1 parent ad817c5 commit 6ed44a5

File tree

2 files changed

+109
-22
lines changed

2 files changed

+109
-22
lines changed

src/jBinary.js

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ if (!('atob' in global) || !('btoa' in global)) {
88
(function(){var t=global,r="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",n=function(){try{document.createElement("$")}catch(t){return t}}();t.btoa||(t.btoa=function(t){for(var o,e,a=0,c=r,f="";t.charAt(0|a)||(c="=",a%1);f+=c.charAt(63&o>>8-8*(a%1))){if(e=t.charCodeAt(a+=.75),e>255)throw n;o=o<<8|e}return f}),t.atob||(t.atob=function(t){if(t=t.replace(/=+$/,""),1==t.length%4)throw n;for(var o,e,a=0,c=0,f="";e=t.charAt(c++);~e&&(o=a%4?64*o+e:e,a++%4)?f+=String.fromCharCode(255&o>>(6&-2*a)):0)e=r.indexOf(e);return f})})();
99
}
1010

11+
12+
// http://cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.min.js
13+
if (!('JSON' in global)) {
14+
// jshint:skipline
15+
JSON={};(function(){function k(a){return a<10?"0"+a:a}function o(a){p.lastIndex=0;return p.test(a)?'"'+a.replace(p,function(a){var c=r[a];return typeof c==="string"?c:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function l(a,j){var c,d,h,m,g=e,f,b=j[a];b&&typeof b==="object"&&typeof b.toJSON==="function"&&(b=b.toJSON(a));typeof i==="function"&&(b=i.call(j,a,b));switch(typeof b){case "string":return o(b);case "number":return isFinite(b)?String(b):"null";case "boolean":case "null":return String(b);case "object":if(!b)return"null";e+=n;f=[];if(Object.prototype.toString.apply(b)==="[object Array]"){m=b.length;for(c=0;c<m;c+=1)f[c]=l(c,b)||"null";h=f.length===0?"[]":e?"[\n"+e+f.join(",\n"+e)+"\n"+g+"]":"["+f.join(",")+"]";e=g;return h}if(i&&typeof i==="object"){m=i.length;for(c=0;c<m;c+=1)typeof i[c]==="string"&&(d=i[c],(h=l(d,b))&&f.push(o(d)+(e?": ":":")+h))}else for(d in b)Object.prototype.hasOwnProperty.call(b,d)&&(h=l(d,b))&&f.push(o(d)+(e?": ":":")+h);h=f.length===0?"{}":e?"{\n"+e+f.join(",\n"+e)+"\n"+g+"}":"{"+f.join(",")+"}";e=g;return h}}if(typeof Date.prototype.toJSON!=="function")Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+k(this.getUTCMonth()+1)+"-"+k(this.getUTCDate())+"T"+k(this.getUTCHours())+":"+k(this.getUTCMinutes())+":"+k(this.getUTCSeconds())+"Z":null},String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){return this.valueOf()};var q=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,p=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,e,n,r={"\u0008":"\\b","\t":"\\t","\n":"\\n","\u000c":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},i;if(typeof JSON.stringify!=="function")JSON.stringify=function(a,j,c){var d;n=e="";if(typeof c==="number")for(d=0;d<c;d+=1)n+=" ";else typeof c==="string"&&(n=c);if((i=j)&&typeof j!=="function"&&(typeof j!=="object"||typeof j.length!=="number"))throw Error("JSON.stringify");return l("",{"":a})};if(typeof JSON.parse!=="function")JSON.parse=function(a,e){function c(a,d){var g,f,b=a[d];if(b&&typeof b==="object")for(g in b)Object.prototype.hasOwnProperty.call(b,g)&&(f=c(b,g),f!==void 0?b[g]=f:delete b[g]);return e.call(a,d,b)}var d,a=String(a);q.lastIndex=0;q.test(a)&&(a=a.replace(q,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)}));if(/^[\],:{}\s]*$/.test(a.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,"")))return d=eval("("+a+")"),typeof e==="function"?c({"":d},""):d;throw new SyntaxError("JSON.parse");}})();
16+
}
17+
1118
var hasRequire = typeof require === 'function';
1219

1320
var jDataView;
@@ -68,22 +75,28 @@ proto.cacheKey = 'jBinary.Cache';
6875
proto.id = 0;
6976

7077
var defineProperty = Object.defineProperty;
71-
// this is needed to detect broken Object.defineProperty in IE8:
72-
try {
73-
defineProperty({}, 'x', {});
74-
} catch (e) {
75-
defineProperty = null;
78+
79+
if (defineProperty) {
80+
// this is needed to detect broken Object.defineProperty in IE8:
81+
try {
82+
defineProperty({}, 'x', {});
83+
} catch (e) {
84+
defineProperty = null;
85+
}
86+
}
87+
88+
if (!defineProperty) {
89+
defineProperty = function (obj, key, descriptor, allowVisible) {
90+
if (allowVisible) {
91+
obj[key] = descriptor.value;
92+
}
93+
};
7694
}
7795

7896
proto._getCached = function (obj, valueAccessor, allowVisible) {
7997
if (!obj.hasOwnProperty(this.cacheKey)) {
8098
var value = valueAccessor.call(this, obj);
81-
if (defineProperty) {
82-
defineProperty(obj, this.cacheKey, {value: value});
83-
} else
84-
if (allowVisible) {
85-
obj[this.cacheKey] = value;
86-
}
99+
defineProperty(obj, this.cacheKey, {value: value}, allowVisible);
87100
return value;
88101
} else {
89102
return obj[this.cacheKey];
@@ -709,17 +722,19 @@ var getScript = (function () {
709722
script.src = url;
710723
script.defer = true;
711724

712-
if (!('onload' in script)) {
713-
script.onreadystatechange = function () {
714-
console.log(this.src, ' ', this.readyState);
715-
if (this.readyState === 'loaded' || this.readyState === 'complete') {
716-
this.onreadystatechange = null;
717-
this.onload();
718-
}
719-
};
720-
}
725+
if (callback) {
726+
if (!('onload' in script)) {
727+
script.onreadystatechange = function () {
728+
console.log(this.src, ' ', this.readyState);
729+
if (this.readyState === 'loaded' || this.readyState === 'complete') {
730+
this.onreadystatechange = null;
731+
this.onload();
732+
}
733+
};
734+
}
721735

722-
script.onload = script.onerror = callback;
736+
script.onload = script.onerror = callback;
737+
}
723738

724739
head.appendChild(script);
725740
};
@@ -733,7 +748,9 @@ var getScript = (function () {
733748
// jshint:skipline
734749
eval(body);
735750
}
736-
callback();
751+
if (callback) {
752+
callback();
753+
}
737754
});
738755
};
739756
}
@@ -776,6 +793,42 @@ var repo = jBinary.Repo = function (names, callback) {
776793
});
777794
};
778795

796+
repo.getAssociations = function (callback) {
797+
// lazy loading data by replacing `jBinary.Repo.getAssociations` itself
798+
getScript('https://rawgithub.com/jDataView/jBinary.Repo/gh-pages/associations.js', function () {
799+
repo.getAssociations(callback);
800+
});
801+
};
802+
803+
repo.getAssociation = function (source, _callback) {
804+
var callback = function (typeSetName) {
805+
repo(typeSetName, _callback);
806+
};
807+
808+
repo.getAssociations(function (assoc) {
809+
if (source.fileName) {
810+
var fileParts = source.fileName.toLowerCase().split('.').slice(1);
811+
// trying everything from longest possible extension to shortest one
812+
for (var i = 0, length = fileParts.length; i < length; i++) {
813+
var extension = fileParts.slice(i).join('.'),
814+
typeSetName = assoc.extensions[extension];
815+
816+
if (typeSetName) {
817+
return callback(typeSetName);
818+
}
819+
}
820+
}
821+
if (source.mimeType) {
822+
var typeSetName = assoc.mimeTypes[source.mimeType];
823+
824+
if (typeSetName) {
825+
return callback(typeSetName);
826+
}
827+
}
828+
_callback();
829+
});
830+
};
831+
779832
if (typeof module === 'object' && module && typeof module.exports === 'object') {
780833
jDataView = require('jDataView');
781834
module.exports = jBinary;

test/test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ if (typeof JSHINT !== 'undefined') {
5656
}
5757
ok(false, 'Line ' + error.line + ', character ' + error.character + ': ' + error.reason);
5858
console.log(error);
59+
} else {
60+
errorCount--;
5961
}
6062
}
6163
if (!errorCount) {
@@ -236,6 +238,38 @@ asyncTest('Cached type', function () {
236238
});
237239
});
238240

241+
module('File associations');
242+
243+
asyncTest('Loading list', function () {
244+
jBinary.Repo.getAssociations(function (assoc) {
245+
start();
246+
ok(assoc);
247+
ok(assoc.extensions);
248+
ok(assoc.mimeTypes);
249+
250+
// check caching
251+
stop();
252+
jBinary.Repo.getAssociations(function (assoc2) {
253+
start();
254+
equal(assoc, assoc2);
255+
});
256+
});
257+
});
258+
259+
asyncTest('By file extension', function () {
260+
jBinary.Repo.getAssociation({fileName: 'sample.mp3'}, function (typeSet) {
261+
start();
262+
equal(typeSet, jBinary.Repo.MP3);
263+
});
264+
});
265+
266+
asyncTest('By mime-type', function () {
267+
jBinary.Repo.getAssociation({mimeType: 'image/bmp'}, function (typeSet) {
268+
start();
269+
equal(typeSet, jBinary.Repo.BMP);
270+
});
271+
});
272+
239273
//-----------------------------------------------------------------
240274

241275
module('Value Read', {

0 commit comments

Comments
 (0)