-
Notifications
You must be signed in to change notification settings - Fork 41
/
turing.core.js
151 lines (134 loc) · 3.83 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*!
* Turing Core
* Copyright (C) 2010-2011 Alex R. Young
* MIT Licensed
*/
/**
* The core Turing module.
*/
(function(global) {
var middleware = [], turing, modules = {};
/**
* The turing object. Use `turing('selector')` for quick DOM access when built with the DOM module.
*
* @returns {Object} The turing object, run through `init`
*/
turing = function() {
var result, i;
for (i = 0; i < middleware.length; i++) {
result = middleware[i].apply(turing, arguments);
if (result) {
return result;
}
}
}
turing.VERSION = '0.0.90';
turing.lesson = 'Part 90: AMD';
/**
* This alias will be used as an alternative to `turing()`.
* If `__turing_alias` is present in the global scope this will be used instead.
*
*/
if (typeof window !== 'undefined') {
turing.alias = window.__turing_alias || '$t';
window[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 = [], i;
for (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];
};
turing.define = function(module, dependencies, fn) {
if (typeof define === 'function' && define.amd) {
define(module, dependencies, fn);
} else {
if (dependencies && dependencies.length) {
for (var i = 0; i < dependencies.length; i++) {
dependencies[i] = modules[dependencies[i]];
}
}
modules[module] = fn.apply(this, dependencies || []);
}
};
/**
* Export `turing` based on environment.
*/
global.turing = turing;
if (typeof exports !== 'undefined') {
exports.turing = turing;
}
turing.define('turing.core', [], function() { return turing; } );
if (typeof define === 'undefined') {
global.define = turing.define;
}
}(typeof window === 'undefined' ? this : window));