Codebase list node-has-binary / 899bdbe
Import upstream version 0.1.7+git20170301.1.f9be9b3 Debian Janitor 2 years ago
5 changed file(s) with 74 addition(s) and 72 deletion(s). Raw diff Collapse all Expand all
+0
-15
.gitignore less more
0 lib-cov
1 *.seed
2 *.log
3 *.csv
4 *.dat
5 *.out
6 *.pid
7 *.gz
8
9 pids
10 logs
11 results
12
13 npm-debug.log
14 node_modules
0 language: node_js
1 sudo: false
2 node_js:
3 - "node"
0 /* global Blob File */
01
12 /*
23 * Module requirements.
1314 /**
1415 * Checks for binary data.
1516 *
16 * Right now only Buffer and ArrayBuffer are supported..
17 * Supports Buffer, ArrayBuffer, Blob and File.
1718 *
1819 * @param {Object} anything
1920 * @api public
2021 */
2122
22 function hasBinary(data) {
23
24 function _hasBinary(obj) {
25 if (!obj) return false;
26
27 if ( (global.Buffer && global.Buffer.isBuffer && global.Buffer.isBuffer(obj)) ||
28 (global.ArrayBuffer && obj instanceof ArrayBuffer) ||
29 (global.Blob && obj instanceof Blob) ||
30 (global.File && obj instanceof File)
31 ) {
32 return true;
33 }
34
35 if (isArray(obj)) {
36 for (var i = 0; i < obj.length; i++) {
37 if (_hasBinary(obj[i])) {
38 return true;
39 }
40 }
41 } else if (obj && 'object' == typeof obj) {
42 // see: https://github.com/Automattic/has-binary/pull/4
43 if (obj.toJSON && 'function' == typeof obj.toJSON) {
44 obj = obj.toJSON();
45 }
46
47 for (var key in obj) {
48 if (Object.prototype.hasOwnProperty.call(obj, key) && _hasBinary(obj[key])) {
49 return true;
50 }
51 }
52 }
53
23 function hasBinary (obj) {
24 if (!obj || typeof obj !== 'object') {
5425 return false;
5526 }
5627
57 return _hasBinary(data);
28 if (isArray(obj)) {
29 for (var i = 0, l = obj.length; i < l; i++) {
30 if (hasBinary(obj[i])) {
31 return true;
32 }
33 }
34 return false;
35 }
36
37 if ((typeof global.Buffer === 'function' && global.Buffer.isBuffer && global.Buffer.isBuffer(obj)) ||
38 (typeof global.ArrayBuffer === 'function' && obj instanceof ArrayBuffer) ||
39 (typeof global.Blob === 'function' && obj instanceof Blob) ||
40 (typeof global.File === 'function' && obj instanceof File)
41 ) {
42 return true;
43 }
44
45 // see: https://github.com/Automattic/has-binary/pull/4
46 if (obj.toJSON && typeof obj.toJSON === 'function') {
47 return hasBinary(obj.toJSON());
48 }
49
50 for (var key in obj) {
51 if (Object.prototype.hasOwnProperty.call(obj, key) && hasBinary(obj[key])) {
52 return true;
53 }
54 }
55
56 return false;
5857 }
22 "version": "0.1.7",
33 "description": "A function that takes anything in javascript and returns true if its argument contains binary data.",
44 "dependencies": {
5 "isarray": "0.0.1"
5 "isarray": "2.0.1"
66 },
77 "devDependencies": {
8 "better-assert": "1.0.0",
9 "mocha": "2.3.4"
8 "better-assert": "^1.0.2",
9 "mocha": "^3.2.0",
10 "semistandard": "^9.2.1"
11 },
12 "scripts": {
13 "checkstyle": "semistandard",
14 "test": "npm run checkstyle && mocha --bail"
1015 },
1116 "author": "Kevin Roark",
1217 "license": "MIT"
0 /* global describe it Blob */
01
12 var hasBinary = require('./');
23 var assert = require('better-assert');
34 var fs = require('fs');
45
5 describe('has-binarydata', function(){
6
7 it('should work with buffer', function(){
6 describe('has-binarydata', function () {
7 it('should work with buffer', function () {
88 assert(hasBinary(fs.readFileSync('./test.js')));
99 });
1010
11 it('should work with an array that does not contain binary', function() {
11 it('should work with an array that does not contain binary', function () {
1212 var arr = [1, 'cool', 2];
1313 assert(!hasBinary(arr));
1414 });
1515
16 it('should work with an array that contains a buffer', function() {
16 it('should work with an array that contains a buffer', function () {
1717 var arr = [1, new Buffer('asdfasdf', 'utf8'), 2];
1818 assert(hasBinary(arr));
1919 });
2020
21 it('should work with an object that does not contain binary', function() {
22 var ob = {a: 'a', b: [], c: 1234, toJSON: '{\"a\": \"a\"}'};
21 it('should work with an object that does not contain binary', function () {
22 var ob = {a: 'a', b: [], c: 1234, toJSON: '{"a": "a"}'};
2323 assert(!hasBinary(ob));
2424 });
2525
26 it('should work with an object that contains a buffer', function() {
27 var ob = {a: 'a', b: new Buffer('abc'), c: 1234, toJSON: '{\"a\": \"a\"}'};
26 it('should work with an object that contains a buffer', function () {
27 var ob = {a: 'a', b: new Buffer('abc'), c: 1234, toJSON: '{"a": "a"}'};
2828 assert(hasBinary(ob));
2929 });
3030
31 it('should work with null', function() {
31 it('should work with an object whose toJSON() returns a buffer', function () {
32 var ob = {a: 'a', b: [], c: 1234, toJSON: function () { return new Buffer('abc'); }};
33 assert(hasBinary(ob));
34 });
35
36 it('should work with null', function () {
3237 assert(!hasBinary(null));
3338 });
3439
35 it('should work with undefined', function() {
40 it('should work with undefined', function () {
3641 assert(!hasBinary(undefined));
3742 });
3843
39 it('should work with a complex object that contains undefined and no binary', function() {
44 it('should work with a complex object that contains undefined and no binary', function () {
4045 var ob = {
4146 x: ['a', 'b', 123],
4247 y: undefined,
4651 assert(!hasBinary(ob));
4752 });
4853
49 it('should work with a complex object that contains undefined and binary', function() {
54 it('should work with a complex object that contains undefined and binary', function () {
5055 var ob = {
5156 x: ['a', 'b', 123],
5257 y: undefined,
5863 });
5964
6065 if (global.ArrayBuffer) {
61 it('should work with an ArrayBuffer', function() {
62 assert(hasBinary(new ArrayBuffer()));
63 });
66 it('should work with an ArrayBuffer', function () {
67 assert(hasBinary(new ArrayBuffer()));
68 });
6469 }
6570
6671 if (global.Blob) {
67 it('should work with a Blob', function() {
68 assert(hasBinary(new Blob()));
69 });
72 it('should work with a Blob', function () {
73 assert(hasBinary(new Blob()));
74 });
75 } else {
76 it('should not crash if global Blob is not a function', function () {
77 global.Blob = [ 1, 2, 3 ];
78 assert(!hasBinary(global.Blob));
79 });
7080 }
71
7281 });