forked from alexyoung/turing.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turing.core.js
129 lines (114 loc) · 3.32 KB
/
turing.core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*!
* Turing Core
* Copyright (C) 2010-2011 Alex R. Young
* MIT Licensed
*/
/**
* A private namespace to set things up against the global object.
*/
(function(global) {
var middleware = [];
/**
* The turing object. Use `turing('selector')` for quick DOM access when built with the DOM module.
*
* @returns {Object} The turing object, run through `init`
*/
function turing() {
if (arguments.length > 0) {
var result;
for (var i = 0; i < middleware.length; i++) {
result = middleware[i].apply(turing, arguments);
if (result) return result;
}
}
}
turing.VERSION = '0.0.64';
turing.lesson = 'Part 64: Properties';
/**
* This alias will be used as an alternative to `turing()`.
* If `__turing_alias` is present in the global scope this will be used instead.
*
*/
turing.alias = global.__turing_alias || '$t';
global[turing.alias] = turing;
/**
* Determine if an object is an `Array`.
*
* @param {Object} object An object that may or may not be an array
* @returns {Boolean} True if the parameter is an array
*/
turing.isArray = Array.isArray || function(object) {
return !!(object && object.concat
&& object.unshift && !object.callee);
};
/**
* Convert an `Array`-like collection into an `Array`.
*
* @param {Object} collection A collection of items that responds to length
* @returns {Array} An `Array` of items
*/
turing.toArray = function(collection) {
var results = [];
for (var i = 0; i < collection.length; i++) {
results.push(collection[i]);
}
return results;
};
// This can be overriden by libraries that extend turing(...)
turing.init = function(fn) {
middleware.unshift(fn);
};
/**
* Determines if an object is a `Number`.
*
* @param {Object} object A value to test
* @returns {Boolean} True if the object is a Number
*/
turing.isNumber = function(object) {
return (object === +object) || (toString.call(object) === '[object Number]');
};
/**
* Binds a function to an object.
*
* @param {Function} fn A function
* @param {Object} object An object to bind to
* @returns {Function} A rebound method
*/
turing.bind = function(fn, object) {
var slice = Array.prototype.slice,
args = slice.apply(arguments, [2]);
return function() {
return fn.apply(object || {}, args.concat(slice.apply(arguments)));
};
};
var testCache = {},
detectionTests = {};
/**
* Used to add feature-detection methods.
*
* @param {String} name The name of the test
* @param {Function} fn The function that performs the test
*/
turing.addDetectionTest = function(name, fn) {
if (!detectionTests[name])
detectionTests[name] = fn;
};
/**
* Run a feature detection name.
*
* @param {String} name The name of the test
* @returns {Boolean} The outcome of the test
*/
turing.detect = function(testName) {
if (typeof testCache[testCache] === 'undefined') {
testCache[testName] = detectionTests[testName]();
}
return testCache[testName];
};
if (global.turing) {
throw new Error('turing has already been defined');
} else {
global.turing = turing;
if (typeof exports !== 'undefined') exports.turing = turing;
}
})(typeof window === 'undefined' ? this : window);