cornerstone-source 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/Gemfile +1 -2
  2. data/Rakefile +0 -6
  3. data/cornerstone-source.gemspec +5 -11
  4. data/lib/cornerstone-source/sprockets.rb +1 -1
  5. data/lib/cornerstone-source/version.rb +1 -1
  6. data/source/{javascripts/_cornerstone/_object_extensions.js.coffee → _object_extensions.js.coffee} +0 -0
  7. data/source/{javascripts/_cornerstone/array_extensions.js.coffee → array_extensions.js.coffee} +28 -26
  8. data/source/{javascripts/_cornerstone/bindable.js.coffee → bindable.js.coffee} +0 -0
  9. data/source/{javascripts/_cornerstone/command_stack.js.coffee → command_stack.js.coffee} +0 -0
  10. data/source/{javascripts/_cornerstone/core_object.js.coffee → core_object.js.coffee} +0 -0
  11. data/source/{javascripts/_cornerstone/function_extensions.js.coffee → function_extensions.js.coffee} +0 -0
  12. data/source/{javascripts/_cornerstone/logging.js.coffee → logging.js.coffee} +0 -0
  13. data/source/{javascripts/_cornerstone/matrix.js.coffee → matrix.js.coffee} +0 -0
  14. data/source/{javascripts/_cornerstone/number_extensions.js.coffee → number_extensions.js.coffee} +0 -0
  15. data/source/{javascripts/_cornerstone/point.js.coffee → point.js.coffee} +0 -0
  16. data/source/{javascripts/_cornerstone/random.js.coffee → random.js.coffee} +0 -0
  17. data/source/{javascripts/_cornerstone/rectangle.js.coffee → rectangle.js.coffee} +0 -0
  18. data/source/{javascripts/_cornerstone/string_extensions.js.coffee → string_extensions.js.coffee} +3 -0
  19. data/source/{javascripts/_cornerstone/uuid.js → uuid.js} +0 -0
  20. data/{source/javascripts/_test → test}/array_extensions.coffee +0 -0
  21. data/{source/javascripts/_test → test}/bindable.coffee +0 -0
  22. data/{source/javascripts/_test → test}/command_stack.coffee +0 -0
  23. data/{source/javascripts/_test → test}/core_object.coffee +0 -0
  24. data/{source/javascripts/_test → test}/function_extensions.coffee +0 -0
  25. data/{source/javascripts/_test → test}/logging.coffee +0 -0
  26. data/{source/javascripts/_test → test}/matrix.coffee +0 -0
  27. data/{source/javascripts/_test → test}/number_extensions.coffee +0 -0
  28. data/{source/javascripts/_test → test}/object_extensions.coffee +0 -0
  29. data/{source/javascripts/_test → test}/point.coffee +0 -0
  30. data/{source/javascripts/_test → test}/random.coffee +0 -0
  31. data/{source/javascripts/_test → test}/rectangle.coffee +0 -0
  32. data/{source/javascripts/_test → test}/string_extensions.coffee +10 -0
  33. metadata +37 -113
  34. data/config.rb +0 -79
  35. data/config.ru +0 -4
  36. data/doc_scraper.rb +0 -51
  37. data/game.js +0 -4295
  38. data/lib/assets/javascripts/cornerstone.js +0 -4722
  39. data/manifest.json +0 -15
  40. data/pixie.json +0 -12
  41. data/source/javascripts/_cornerstone/stubs.js.coffee +0 -1042
  42. data/source/javascripts/cornerstone.js.coffee +0 -1
  43. data/source/javascripts/cornerstone_tests.js.coffee +0 -2
  44. data/source/test.html.haml +0 -13
data/config.rb DELETED
@@ -1,79 +0,0 @@
1
- ###
2
- # Compass
3
- ###
4
-
5
- # Susy grids in Compass
6
- # First: gem install compass-susy-plugin
7
- # require 'susy'
8
-
9
- # Change Compass configuration
10
- # compass_config do |config|
11
- # config.output_style = :compact
12
- # end
13
-
14
- ###
15
- # Page options, layouts, aliases and proxies
16
- ###
17
-
18
- # Per-page layout changes:
19
- #
20
- # With no layout
21
- # page "/path/to/file.html", :layout => false
22
- #
23
- # With alternative layout
24
- # page "/path/to/file.html", :layout => :otherlayout
25
- #
26
- # A path which all have the same layout
27
- # with_layout :admin do
28
- # page "/admin/*"
29
- # end
30
-
31
- # Proxy (fake) files
32
- # page "/this-page-has-no-template.html", :proxy => "/template-file.html" do
33
- # @which_fake_page = "Rendering a fake page with a variable"
34
- # end
35
-
36
- ###
37
- # Helpers
38
- ###
39
-
40
- # Automatic image dimensions on image_tag helper
41
- # activate :automatic_image_sizes
42
-
43
- # Methods defined in the helpers block are available in templates
44
- # helpers do
45
- # def some_helper
46
- # "Helping"
47
- # end
48
- # end
49
-
50
- set :css_dir, 'stylesheets'
51
-
52
- set :js_dir, 'javascripts'
53
-
54
- set :images_dir, 'images'
55
-
56
- set :build_dir, 'lib/assets'
57
-
58
- # Build-specific configuration
59
- configure :build do
60
- # For example, change the Compass output style for deployment
61
- # activate :minify_css
62
-
63
- # Minify Javascript on build
64
- # activate :minify_javascript
65
-
66
- # Enable cache buster
67
- # activate :cache_buster
68
-
69
- # Use relative URLs
70
- # activate :relative_assets
71
-
72
- # Compress PNGs after build
73
- # First: gem install middleman-smusher
74
- # require "middleman-smusher"
75
- # activate :smusher
76
-
77
- # Or use a different image path
78
- # set :http_path, "/Content/images/"
79
- end
data/config.ru DELETED
@@ -1,4 +0,0 @@
1
- require 'rubygems'
2
- require 'middleman'
3
-
4
- run Middleman.server
@@ -1,51 +0,0 @@
1
- require 'rubygems'
2
- require 'mechanize'
3
- require 'action_view'
4
-
5
- include ActionView::Helpers::TextHelper
6
-
7
- agent = Mechanize.new
8
-
9
- method_xpath = "/html/body/section[2]/div/div/article/div[2]/div[2]/div[2]/div[7]/div[2]/div/dl/dt/a | /html/body/section[2]/div/div/article/div[2]/div[2]/div[2]/div[7]/div[2]/dl/dt/a | /html/body/section[2]/div/div/article/div[2]/div[2]/div[2]/div[6]/div[3]/dl/dt/a | /html/body/section[2]/div/div/article/div[2]/div[2]/div[2]/div[7]/dl/dt/a"
10
- description_xpath = "/html/body/section[2]/div/div/article/div[2]/div[2]/div[2]/div[1]/p"
11
- usage_path = "/html/body/section[2]/div/div/article/div[2]/div[2]/div[2]/div[2]/p/code"
12
- param_xpath = "/html/body/section[2]/div/div/article/div[2]/div[2]/div[2]/div[3]/dl"
13
-
14
- objects = %w[Boolean Number String Array Object Function RegExp Date]
15
-
16
- File.open("src/stubs.coffee", 'wb') do |file|
17
- objects.each do |object|
18
- page = agent.get "https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/#{object}"
19
-
20
- method_links = page.search method_xpath
21
-
22
- method_links.each do |method_link|
23
- method_name = method_link.content
24
-
25
- method_page = agent.click(method_link)
26
-
27
- description = method_page.search(description_xpath).text
28
-
29
- example_usage = method_page.search(usage_path).to_s
30
-
31
- params = method_page.search(param_xpath).map do |param|
32
- word_wrap "@param #{param.search("dt").text} #{param.search("dd").text}"
33
- end.join("\n")
34
-
35
- documentation = <<-eof
36
- ###*
37
- #{word_wrap description}
38
-
39
- #{example_usage}
40
- #{params}
41
- @name #{method_name}
42
- @methodOf #{object}#
43
- ###
44
- eof
45
-
46
- puts documentation
47
-
48
- file.write documentation
49
- end
50
- end
51
- end
data/game.js DELETED
@@ -1,4295 +0,0 @@
1
-
2
-
3
- /**
4
- Checks whether an object is an array.
5
-
6
- Object.isArray([1, 2, 4])
7
- # => true
8
-
9
- Object.isArray({key: "value"})
10
- # => false
11
-
12
- @name isArray
13
- @methodOf Object
14
- @param {Object} object The object to check for array-ness.
15
- @returns {Boolean} A boolean expressing whether the object is an instance of Array
16
- */
17
- var __slice = Array.prototype.slice;
18
-
19
- Object.isArray = function(object) {
20
- return Object.prototype.toString.call(object) === "[object Array]";
21
- };
22
-
23
- /**
24
- Checks whether an object is a string.
25
-
26
- Object.isString("a string")
27
- # => true
28
-
29
- Object.isString([1, 2, 4])
30
- # => false
31
-
32
- Object.isString({key: "value"})
33
- # => false
34
-
35
- @name isString
36
- @methodOf Object
37
- @param {Object} object The object to check for string-ness.
38
- @returns {Boolean} A boolean expressing whether the object is an instance of String
39
- */
40
-
41
- Object.isString = function(object) {
42
- return Object.prototype.toString.call(object) === "[object String]";
43
- };
44
-
45
- /**
46
- Merges properties from objects into target without overiding.
47
- First come, first served.
48
-
49
- I =
50
- a: 1
51
- b: 2
52
- c: 3
53
-
54
- Object.reverseMerge I,
55
- c: 6
56
- d: 4
57
-
58
- I # => {a: 1, b:2, c:3, d: 4}
59
-
60
- @name reverseMerge
61
- @methodOf Object
62
- @param {Object} target The object to merge the properties into.
63
- @returns {Object} target
64
- */
65
-
66
- Object.defaults = Object.reverseMerge = function() {
67
- var name, object, objects, target, _i, _len;
68
- target = arguments[0], objects = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
69
- for (_i = 0, _len = objects.length; _i < _len; _i++) {
70
- object = objects[_i];
71
- for (name in object) {
72
- if (!target.hasOwnProperty(name)) target[name] = object[name];
73
- }
74
- }
75
- return target;
76
- };
77
-
78
- /**
79
- Merges properties from sources into target with overiding.
80
- Last in covers earlier properties.
81
-
82
- I =
83
- a: 1
84
- b: 2
85
- c: 3
86
-
87
- Object.extend I,
88
- c: 6
89
- d: 4
90
-
91
- I # => {a: 1, b:2, c:6, d: 4}
92
-
93
- @name extend
94
- @methodOf Object
95
- @param {Object} target The object to merge the properties into.
96
- @returns {Object} target
97
- */
98
-
99
- Object.extend = function() {
100
- var name, source, sources, target, _i, _len;
101
- target = arguments[0], sources = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
102
- for (_i = 0, _len = sources.length; _i < _len; _i++) {
103
- source = sources[_i];
104
- for (name in source) {
105
- target[name] = source[name];
106
- }
107
- }
108
- return target;
109
- };
110
-
111
- /**
112
- Helper method that tells you if something is an object.
113
-
114
- object = {a: 1}
115
-
116
- Object.isObject(object)
117
- # => true
118
-
119
- @name isObject
120
- @methodOf Object
121
- @param {Object} object Maybe this guy is an object.
122
- @returns {Boolean} true if this guy is an object.
123
- */
124
-
125
- Object.isObject = function(object) {
126
- return Object.prototype.toString.call(object) === '[object Object]';
127
- };
128
-
129
- /**
130
- Calculate the average value of an array. Returns undefined if some elements
131
- are not numbers.
132
-
133
- [1, 3, 5, 7].average()
134
- # => 4
135
-
136
- @name average
137
- @methodOf Array#
138
- @returns {Number} The average (arithmetic mean) of the list of numbers.
139
- */
140
- var _base,
141
- __slice = Array.prototype.slice;
142
-
143
- Array.prototype.average = function() {
144
- return this.sum() / this.length;
145
- };
146
-
147
- /**
148
- Returns a copy of the array without null and undefined values.
149
-
150
- [null, undefined, 3, 3, undefined, 5].compact()
151
- # => [3, 3, 5]
152
-
153
- @name compact
154
- @methodOf Array#
155
- @returns {Array} A new array that contains only the non-null values.
156
- */
157
-
158
- Array.prototype.compact = function() {
159
- return this.select(function(element) {
160
- return element != null;
161
- });
162
- };
163
-
164
- /**
165
- Creates and returns a copy of the array. The copy contains
166
- the same objects.
167
-
168
- a = ["a", "b", "c"]
169
- b = a.copy()
170
-
171
- # their elements are equal
172
- a[0] == b[0] && a[1] == b[1] && a[2] == b[2]
173
- # => true
174
-
175
- # but they aren't the same object in memory
176
- a === b
177
- # => false
178
-
179
- @name copy
180
- @methodOf Array#
181
- @returns {Array} A new array that is a copy of the array
182
- */
183
-
184
- Array.prototype.copy = function() {
185
- return this.concat();
186
- };
187
-
188
- /**
189
- Empties the array of its contents. It is modified in place.
190
-
191
- fullArray = [1, 2, 3]
192
- fullArray.clear()
193
- fullArray
194
- # => []
195
-
196
- @name clear
197
- @methodOf Array#
198
- @returns {Array} this, now emptied.
199
- */
200
-
201
- Array.prototype.clear = function() {
202
- this.length = 0;
203
- return this;
204
- };
205
-
206
- /**
207
- Flatten out an array of arrays into a single array of elements.
208
-
209
- [[1, 2], [3, 4], 5].flatten()
210
- # => [1, 2, 3, 4, 5]
211
-
212
- # won't flatten twice nested arrays. call
213
- # flatten twice if that is what you want
214
- [[1, 2], [3, [4, 5]], 6].flatten()
215
- # => [1, 2, 3, [4, 5], 6]
216
-
217
- @name flatten
218
- @methodOf Array#
219
- @returns {Array} A new array with all the sub-arrays flattened to the top.
220
- */
221
-
222
- Array.prototype.flatten = function() {
223
- return this.inject([], function(a, b) {
224
- return a.concat(b);
225
- });
226
- };
227
-
228
- /**
229
- Invoke the named method on each element in the array
230
- and return a new array containing the results of the invocation.
231
-
232
- [1.1, 2.2, 3.3, 4.4].invoke("floor")
233
- # => [1, 2, 3, 4]
234
-
235
- ['hello', 'world', 'cool!'].invoke('substring', 0, 3)
236
- # => ['hel', 'wor', 'coo']
237
-
238
- @param {String} method The name of the method to invoke.
239
- @param [arg...] Optional arguments to pass to the method being invoked.
240
- @name invoke
241
- @methodOf Array#
242
- @returns {Array} A new array containing the results of invoking the named method on each element.
243
- */
244
-
245
- Array.prototype.invoke = function() {
246
- var args, method;
247
- method = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
248
- return this.map(function(element) {
249
- return element[method].apply(element, args);
250
- });
251
- };
252
-
253
- /**
254
- Randomly select an element from the array.
255
-
256
- [1, 2, 3].rand()
257
- # => 2
258
-
259
- @name rand
260
- @methodOf Array#
261
- @returns {Object} A random element from an array
262
- */
263
-
264
- Array.prototype.rand = function() {
265
- return this[rand(this.length)];
266
- };
267
-
268
- /**
269
- Remove the first occurrence of the given object from the array if it is
270
- present. The array is modified in place.
271
-
272
- a = [1, 1, "a", "b"]
273
- a.remove(1)
274
- # => 1
275
-
276
- a
277
- # => [1, "a", "b"]
278
-
279
- @name remove
280
- @methodOf Array#
281
- @param {Object} object The object to remove from the array if present.
282
- @returns {Object} The removed object if present otherwise undefined.
283
- */
284
-
285
- Array.prototype.remove = function(object) {
286
- var index;
287
- index = this.indexOf(object);
288
- if (index >= 0) {
289
- return this.splice(index, 1)[0];
290
- } else {
291
- return;
292
- }
293
- };
294
-
295
- /**
296
- Returns true if the element is present in the array.
297
-
298
- ["a", "b", "c"].include("c")
299
- # => true
300
-
301
- [40, "a"].include(700)
302
- # => false
303
-
304
- @name include
305
- @methodOf Array#
306
- @param {Object} element The element to check if present.
307
- @returns {Boolean} true if the element is in the array, false otherwise.
308
- */
309
-
310
- Array.prototype.include = function(element) {
311
- return this.indexOf(element) !== -1;
312
- };
313
-
314
- /**
315
- Call the given iterator once for each element in the array,
316
- passing in the element as the first argument, the index of
317
- the element as the second argument, and <code>this</code> array as the
318
- third argument.
319
-
320
- word = ""
321
- indices = []
322
- ["r", "a", "d"].each (letter, index) ->
323
- word += letter
324
- indices.push(index)
325
-
326
- # => ["r", "a", "d"]
327
-
328
- word
329
- # => "rad"
330
-
331
- indices
332
- # => [0, 1, 2]
333
-
334
- @name each
335
- @methodOf Array#
336
- @param {Function} iterator Function to be called once for each element in the array.
337
- @param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
338
- @returns {Array} this to enable method chaining.
339
- */
340
-
341
- Array.prototype.each = function(iterator, context) {
342
- var element, i, _len;
343
- if (this.forEach) {
344
- this.forEach(iterator, context);
345
- } else {
346
- for (i = 0, _len = this.length; i < _len; i++) {
347
- element = this[i];
348
- iterator.call(context, element, i, this);
349
- }
350
- }
351
- return this;
352
- };
353
-
354
- /**
355
- Call the given iterator once for each element in the array,
356
- passing in the element as the first argument, the index of
357
- the element as the second argument, and `this` array as the
358
- third argument.
359
-
360
- [1, 2, 3].map (number) ->
361
- number * number
362
- # => [1, 4, 9]
363
-
364
- @name map
365
- @methodOf Array#
366
- @param {Function} iterator Function to be called once for each element in the array.
367
- @param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
368
- @returns {Array} An array of the results of the iterator function being called on the original array elements.
369
- */
370
-
371
- (_base = Array.prototype).map || (_base.map = function(iterator, context) {
372
- var element, i, results, _len;
373
- results = [];
374
- for (i = 0, _len = this.length; i < _len; i++) {
375
- element = this[i];
376
- results.push(iterator.call(context, element, i, this));
377
- }
378
- return results;
379
- });
380
-
381
- /**
382
- Call the given iterator once for each pair of objects in the array.
383
-
384
- [1, 2, 3, 4].eachPair (a, b) ->
385
- # 1, 2
386
- # 1, 3
387
- # 1, 4
388
- # 2, 3
389
- # 2, 4
390
- # 3, 4
391
-
392
- @name eachPair
393
- @methodOf Array#
394
- @param {Function} iterator Function to be called once for each pair of elements in the array.
395
- @param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
396
- */
397
-
398
- Array.prototype.eachPair = function(iterator, context) {
399
- var a, b, i, j, length, _results;
400
- length = this.length;
401
- i = 0;
402
- _results = [];
403
- while (i < length) {
404
- a = this[i];
405
- j = i + 1;
406
- i += 1;
407
- _results.push((function() {
408
- var _results2;
409
- _results2 = [];
410
- while (j < length) {
411
- b = this[j];
412
- j += 1;
413
- _results2.push(iterator.call(context, a, b));
414
- }
415
- return _results2;
416
- }).call(this));
417
- }
418
- return _results;
419
- };
420
-
421
- /**
422
- Call the given iterator once for each element in the array,
423
- passing in the element as the first argument and the given object
424
- as the second argument. Additional arguments are passed similar to
425
- <code>each</code>.
426
-
427
- @see Array#each
428
- @name eachWithObject
429
- @methodOf Array#
430
- @param {Object} object The object to pass to the iterator on each visit.
431
- @param {Function} iterator Function to be called once for each element in the array.
432
- @param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
433
- @returns {Array} this
434
- */
435
-
436
- Array.prototype.eachWithObject = function(object, iterator, context) {
437
- this.each(function(element, i, self) {
438
- return iterator.call(context, element, object, i, self);
439
- });
440
- return object;
441
- };
442
-
443
- /**
444
- Call the given iterator once for each group of elements in the array,
445
- passing in the elements in groups of n. Additional argumens are
446
- passed as in each.
447
-
448
- results = []
449
- [1, 2, 3, 4].eachSlice 2, (slice) ->
450
- results.push(slice)
451
- # => [1, 2, 3, 4]
452
-
453
- results
454
- # => [[1, 2], [3, 4]]
455
-
456
- @see Array#each
457
- @name eachSlice
458
- @methodOf Array#
459
- @param {Number} n The number of elements in each group.
460
- @param {Function} iterator Function to be called once for each group of elements in the array.
461
- @param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
462
- @returns {Array} this
463
- */
464
-
465
- Array.prototype.eachSlice = function(n, iterator, context) {
466
- var i, len;
467
- if (n > 0) {
468
- len = (this.length / n).floor();
469
- i = -1;
470
- while (++i < len) {
471
- iterator.call(context, this.slice(i * n, (i + 1) * n), i * n, this);
472
- }
473
- }
474
- return this;
475
- };
476
-
477
- /**
478
- Pipe the input through each function in the array in turn. For example, if you have a
479
- list of objects you can perform a series of selection, sorting, and other processing
480
- methods and then receive the processed list. This array must contain functions that
481
- accept a single input and return the processed input. The output of the first function
482
- is fed to the input of the second and so on until the final processed output is returned.
483
-
484
- @name pipeline
485
- @methodOf Array#
486
-
487
- @param {Object} input The initial input to pass to the first function in the pipeline.
488
- @returns {Object} The result of processing the input by each function in the array.
489
- */
490
-
491
- Array.prototype.pipeline = function(input) {
492
- var fn, _i, _len;
493
- for (_i = 0, _len = this.length; _i < _len; _i++) {
494
- fn = this[_i];
495
- input = fn(input);
496
- }
497
- return input;
498
- };
499
-
500
- /**
501
- Returns a new array with the elements all shuffled up.
502
-
503
- a = [1, 2, 3]
504
-
505
- a.shuffle()
506
- # => [2, 3, 1]
507
-
508
- a # => [1, 2, 3]
509
-
510
- @name shuffle
511
- @methodOf Array#
512
- @returns {Array} A new array that is randomly shuffled.
513
- */
514
-
515
- Array.prototype.shuffle = function() {
516
- var shuffledArray;
517
- shuffledArray = [];
518
- this.each(function(element) {
519
- return shuffledArray.splice(rand(shuffledArray.length + 1), 0, element);
520
- });
521
- return shuffledArray;
522
- };
523
-
524
- /**
525
- Returns the first element of the array, undefined if the array is empty.
526
-
527
- ["first", "second", "third"].first()
528
- # => "first"
529
-
530
- @name first
531
- @methodOf Array#
532
- @returns {Object} The first element, or undefined if the array is empty.
533
- */
534
-
535
- Array.prototype.first = function() {
536
- return this[0];
537
- };
538
-
539
- /**
540
- Returns the last element of the array, undefined if the array is empty.
541
-
542
- ["first", "second", "third"].last()
543
- # => "third"
544
-
545
- @name last
546
- @methodOf Array#
547
- @returns {Object} The last element, or undefined if the array is empty.
548
- */
549
-
550
- Array.prototype.last = function() {
551
- return this[this.length - 1];
552
- };
553
-
554
- /**
555
- Returns an object containing the extremes of this array.
556
-
557
- [-1, 3, 0].extremes()
558
- # => {min: -1, max: 3}
559
-
560
- @name extremes
561
- @methodOf Array#
562
- @param {Function} [fn] An optional funtion used to evaluate each element to calculate its value for determining extremes.
563
- @returns {Object} {min: minElement, max: maxElement}
564
- */
565
-
566
- Array.prototype.extremes = function(fn) {
567
- var max, maxResult, min, minResult;
568
- fn || (fn = function(n) {
569
- return n;
570
- });
571
- min = max = void 0;
572
- minResult = maxResult = void 0;
573
- this.each(function(object) {
574
- var result;
575
- result = fn(object);
576
- if (min != null) {
577
- if (result < minResult) {
578
- min = object;
579
- minResult = result;
580
- }
581
- } else {
582
- min = object;
583
- minResult = result;
584
- }
585
- if (max != null) {
586
- if (result > maxResult) {
587
- max = object;
588
- return maxResult = result;
589
- }
590
- } else {
591
- max = object;
592
- return maxResult = result;
593
- }
594
- });
595
- return {
596
- min: min,
597
- max: max
598
- };
599
- };
600
-
601
- /**
602
- Pretend the array is a circle and grab a new array containing length elements.
603
- If length is not given return the element at start, again assuming the array
604
- is a circle.
605
-
606
- [1, 2, 3].wrap(-1)
607
- # => 3
608
-
609
- [1, 2, 3].wrap(6)
610
- # => 1
611
-
612
- ["l", "o", "o", "p"].wrap(0, 16)
613
- # => ["l", "o", "o", "p", "l", "o", "o", "p", "l", "o", "o", "p", "l", "o", "o", "p"]
614
-
615
- @name wrap
616
- @methodOf Array#
617
- @param {Number} start The index to start wrapping at, or the index of the sole element to return if no length is given.
618
- @param {Number} [length] Optional length determines how long result array should be.
619
- @returns {Object} or {Array} The element at start mod array.length, or an array of length elements, starting from start and wrapping.
620
- */
621
-
622
- Array.prototype.wrap = function(start, length) {
623
- var end, i, result;
624
- if (length != null) {
625
- end = start + length;
626
- i = start;
627
- result = [];
628
- while (i++ < end) {
629
- result.push(this[i.mod(this.length)]);
630
- }
631
- return result;
632
- } else {
633
- return this[start.mod(this.length)];
634
- }
635
- };
636
-
637
- /**
638
- Partitions the elements into two groups: those for which the iterator returns
639
- true, and those for which it returns false.
640
-
641
- [evens, odds] = [1, 2, 3, 4].partition (n) ->
642
- n.even()
643
-
644
- evens
645
- # => [2, 4]
646
-
647
- odds
648
- # => [1, 3]
649
-
650
- @name partition
651
- @methodOf Array#
652
- @param {Function} iterator
653
- @param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
654
- @returns {Array} An array in the form of [trueCollection, falseCollection]
655
- */
656
-
657
- Array.prototype.partition = function(iterator, context) {
658
- var falseCollection, trueCollection;
659
- trueCollection = [];
660
- falseCollection = [];
661
- this.each(function(element) {
662
- if (iterator.call(context, element)) {
663
- return trueCollection.push(element);
664
- } else {
665
- return falseCollection.push(element);
666
- }
667
- });
668
- return [trueCollection, falseCollection];
669
- };
670
-
671
- /**
672
- Return the group of elements for which the return value of the iterator is true.
673
-
674
- @name select
675
- @methodOf Array#
676
- @param {Function} iterator The iterator receives each element in turn as the first agument.
677
- @param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
678
- @returns {Array} An array containing the elements for which the iterator returned true.
679
- */
680
-
681
- Array.prototype.select = function(iterator, context) {
682
- return this.partition(iterator, context)[0];
683
- };
684
-
685
- /**
686
- Return the group of elements that are not in the passed in set.
687
-
688
- [1, 2, 3, 4].without ([2, 3])
689
- # => [1, 4]
690
-
691
- @name without
692
- @methodOf Array#
693
- @param {Array} values List of elements to exclude.
694
- @returns {Array} An array containing the elements that are not passed in.
695
- */
696
-
697
- Array.prototype.without = function(values) {
698
- return this.reject(function(element) {
699
- return values.include(element);
700
- });
701
- };
702
-
703
- /**
704
- Return the group of elements for which the return value of the iterator is false.
705
-
706
- @name reject
707
- @methodOf Array#
708
- @param {Function} iterator The iterator receives each element in turn as the first agument.
709
- @param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
710
- @returns {Array} An array containing the elements for which the iterator returned false.
711
- */
712
-
713
- Array.prototype.reject = function(iterator, context) {
714
- return this.partition(iterator, context)[1];
715
- };
716
-
717
- /**
718
- Combines all elements of the array by applying a binary operation.
719
- for each element in the arra the iterator is passed an accumulator
720
- value (memo) and the element.
721
-
722
- @name inject
723
- @methodOf Array#
724
- @returns {Object} The result of a
725
- */
726
-
727
- Array.prototype.inject = function(initial, iterator) {
728
- this.each(function(element) {
729
- return initial = iterator(initial, element);
730
- });
731
- return initial;
732
- };
733
-
734
- /**
735
- Add all the elements in the array.
736
-
737
- [1, 2, 3, 4].sum()
738
- # => 10
739
-
740
- @name sum
741
- @methodOf Array#
742
- @returns {Number} The sum of the elements in the array.
743
- */
744
-
745
- Array.prototype.sum = function() {
746
- return this.inject(0, function(sum, n) {
747
- return sum + n;
748
- });
749
- };
750
-
751
- /**
752
- Multiply all the elements in the array.
753
-
754
- [1, 2, 3, 4].product()
755
- # => 24
756
-
757
- @name product
758
- @methodOf Array#
759
- @returns {Number} The product of the elements in the array.
760
- */
761
-
762
- Array.prototype.product = function() {
763
- return this.inject(1, function(product, n) {
764
- return product * n;
765
- });
766
- };
767
-
768
- /**
769
- Merges together the values of each of the arrays with the values at the corresponding position.
770
-
771
- ['a', 'b', 'c'].zip([1, 2, 3])
772
- # => [['a', 1], ['b', 2], ['c', 3]]
773
-
774
- @name zip
775
- @methodOf Array#
776
- @returns {Array} Array groupings whose values are arranged by their positions in the original input arrays.
777
- */
778
-
779
- Array.prototype.zip = function() {
780
- var args;
781
- args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
782
- return this.map(function(element, index) {
783
- var output;
784
- output = args.map(function(arr) {
785
- return arr[index];
786
- });
787
- output.unshift(element);
788
- return output;
789
- });
790
- };
791
-
792
- /**
793
- Bindable module.
794
-
795
- player = Core
796
- x: 5
797
- y: 10
798
-
799
- player.bind "update", ->
800
- updatePlayer()
801
- # => Uncaught TypeError: Object has no method 'bind'
802
-
803
- player.include(Bindable)
804
-
805
- player.bind "update", ->
806
- updatePlayer()
807
- # => this will call updatePlayer each time through the main loop
808
-
809
- @name Bindable
810
- @module
811
- @constructor
812
- */
813
- var Bindable,
814
- __slice = Array.prototype.slice;
815
-
816
- Bindable = function(I, self) {
817
- var eventCallbacks;
818
- if (I == null) I = {};
819
- eventCallbacks = {};
820
- return {
821
- bind: function() {
822
- var args;
823
- args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
824
- return self.on.apply(self, args);
825
- },
826
- unbind: function() {
827
- var args;
828
- args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
829
- return self.off.apply(self, args);
830
- },
831
- /**
832
- Adds a function as an event listener.
833
-
834
- # this will call coolEventHandler after
835
- # yourObject.trigger "someCustomEvent" is called.
836
- yourObject.on "someCustomEvent", coolEventHandler
837
-
838
- #or
839
- yourObject.on "anotherCustomEvent", ->
840
- doSomething()
841
-
842
- @name on
843
- @methodOf Bindable#
844
- @param {String} event The event to listen to.
845
- @param {Function} callback The function to be called when the specified event
846
- is triggered.
847
- */
848
- on: function(namespacedEvent, callback) {
849
- var event, namespace, _ref;
850
- _ref = namespacedEvent.split("."), event = _ref[0], namespace = _ref[1];
851
- if (namespace) {
852
- callback.__PIXIE || (callback.__PIXIE = {});
853
- callback.__PIXIE[namespace] = true;
854
- }
855
- eventCallbacks[event] || (eventCallbacks[event] = []);
856
- eventCallbacks[event].push(callback);
857
- return this;
858
- },
859
- /**
860
- Removes a specific event listener, or all event listeners if
861
- no specific listener is given.
862
-
863
- # removes the handler coolEventHandler from the event
864
- # "someCustomEvent" while leaving the other events intact.
865
- yourObject.off "someCustomEvent", coolEventHandler
866
-
867
- # removes all handlers attached to "anotherCustomEvent"
868
- yourObject.off "anotherCustomEvent"
869
-
870
- @name off
871
- @methodOf Bindable#
872
- @param {String} event The event to remove the listener from.
873
- @param {Function} [callback] The listener to remove.
874
- */
875
- off: function(namespacedEvent, callback) {
876
- var callbacks, event, key, namespace, _ref;
877
- _ref = namespacedEvent.split("."), event = _ref[0], namespace = _ref[1];
878
- if (event) {
879
- eventCallbacks[event] || (eventCallbacks[event] = []);
880
- if (namespace) {
881
- eventCallbacks[event] = eventCallbacks.select(function(callback) {
882
- var _ref2;
883
- return !(((_ref2 = callback.__PIXIE) != null ? _ref2[namespace] : void 0) != null);
884
- });
885
- } else {
886
- if (callback) {
887
- eventCallbacks[event].remove(callback);
888
- } else {
889
- eventCallbacks[event] = [];
890
- }
891
- }
892
- } else if (namespace) {
893
- for (key in eventCallbacks) {
894
- callbacks = eventCallbacks[key];
895
- eventCallbacks[key] = callbacks.select(function(callback) {
896
- var _ref2;
897
- return !(((_ref2 = callback.__PIXIE) != null ? _ref2[namespace] : void 0) != null);
898
- });
899
- }
900
- }
901
- return this;
902
- },
903
- /**
904
- Calls all listeners attached to the specified event.
905
-
906
- # calls each event handler bound to "someCustomEvent"
907
- yourObject.trigger "someCustomEvent"
908
-
909
- @name trigger
910
- @methodOf Bindable#
911
- @param {String} event The event to trigger.
912
- @param {Array} [parameters] Additional parameters to pass to the event listener.
913
- */
914
- trigger: function() {
915
- var callbacks, event, parameters;
916
- event = arguments[0], parameters = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
917
- callbacks = eventCallbacks[event];
918
- if (callbacks && callbacks.length) {
919
- self = this;
920
- return callbacks.each(function(callback) {
921
- return callback.apply(self, parameters);
922
- });
923
- }
924
- }
925
- };
926
- };
927
-
928
- (typeof exports !== "undefined" && exports !== null ? exports : this)["Bindable"] = Bindable;
929
-
930
- var CommandStack;
931
-
932
- CommandStack = function() {
933
- var index, stack;
934
- stack = [];
935
- index = 0;
936
- return {
937
- execute: function(command) {
938
- stack[index] = command;
939
- command.execute();
940
- return stack.length = index += 1;
941
- },
942
- undo: function() {
943
- var command;
944
- if (this.canUndo()) {
945
- index -= 1;
946
- command = stack[index];
947
- command.undo();
948
- return command;
949
- }
950
- },
951
- redo: function() {
952
- var command;
953
- if (this.canRedo()) {
954
- command = stack[index];
955
- command.execute();
956
- index += 1;
957
- return command;
958
- }
959
- },
960
- current: function() {
961
- return stack[index - 1];
962
- },
963
- canUndo: function() {
964
- return index > 0;
965
- },
966
- canRedo: function() {
967
- return stack[index] != null;
968
- }
969
- };
970
- };
971
-
972
- (typeof exports !== "undefined" && exports !== null ? exports : this)["CommandStack"] = CommandStack;
973
-
974
- /**
975
- The Core class is used to add extended functionality to objects without
976
- extending the object class directly. Inherit from Core to gain its utility
977
- methods.
978
-
979
- @name Core
980
- @constructor
981
-
982
- @param {Object} I Instance variables
983
- */
984
- var __slice = Array.prototype.slice;
985
-
986
- (function() {
987
- var root;
988
- root = typeof exports !== "undefined" && exports !== null ? exports : this;
989
- return root.Core = function(I) {
990
- var Module, moduleName, self, _i, _len, _ref;
991
- if (I == null) I = {};
992
- Object.reverseMerge(I, {
993
- includedModules: []
994
- });
995
- self = {
996
- /**
997
- External access to instance variables. Use of this property should be avoided
998
- in general, but can come in handy from time to time.
999
-
1000
- I =
1001
- r: 255
1002
- g: 0
1003
- b: 100
1004
-
1005
- myObject = Core(I)
1006
-
1007
- # a bad idea most of the time, but it's
1008
- # pretty convenient to have available.
1009
- myObject.I.r
1010
- # => 255
1011
-
1012
- myObject.I.g
1013
- # => 0
1014
-
1015
- myObject.I.b
1016
- # => 100
1017
-
1018
- @name I
1019
- @fieldOf Core#
1020
- */
1021
- I: I,
1022
- /**
1023
- Generates a public jQuery style getter / setter method for each
1024
- String argument.
1025
-
1026
- myObject = Core
1027
- r: 255
1028
- g: 0
1029
- b: 100
1030
-
1031
- myObject.attrAccessor "r", "g", "b"
1032
-
1033
- myObject.r(254)
1034
- myObject.r()
1035
-
1036
- => 254
1037
-
1038
- @name attrAccessor
1039
- @methodOf Core#
1040
- */
1041
- attrAccessor: function() {
1042
- var attrNames;
1043
- attrNames = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
1044
- return attrNames.each(function(attrName) {
1045
- return self[attrName] = function(newValue) {
1046
- if (newValue != null) {
1047
- I[attrName] = newValue;
1048
- return self;
1049
- } else {
1050
- return I[attrName];
1051
- }
1052
- };
1053
- });
1054
- },
1055
- /**
1056
- Generates a public jQuery style getter method for each String argument.
1057
-
1058
- myObject = Core
1059
- r: 255
1060
- g: 0
1061
- b: 100
1062
-
1063
- myObject.attrReader "r", "g", "b"
1064
-
1065
- myObject.r()
1066
- => 255
1067
-
1068
- myObject.g()
1069
- => 0
1070
-
1071
- myObject.b()
1072
- => 100
1073
-
1074
- @name attrReader
1075
- @methodOf Core#
1076
- */
1077
- attrReader: function() {
1078
- var attrNames;
1079
- attrNames = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
1080
- return attrNames.each(function(attrName) {
1081
- return self[attrName] = function() {
1082
- return I[attrName];
1083
- };
1084
- });
1085
- },
1086
- /**
1087
- Extends this object with methods from the passed in object. A shortcut for Object.extend(self, methods)
1088
-
1089
- I =
1090
- x: 30
1091
- y: 40
1092
- maxSpeed: 5
1093
-
1094
- # we are using extend to give player
1095
- # additional methods that Core doesn't have
1096
- player = Core(I).extend
1097
- increaseSpeed: ->
1098
- I.maxSpeed += 1
1099
-
1100
- player.I.maxSpeed
1101
- => 5
1102
-
1103
- player.increaseSpeed()
1104
-
1105
- player.I.maxSpeed
1106
- => 6
1107
-
1108
- @name extend
1109
- @methodOf Core#
1110
- @see Object.extend
1111
- @returns self
1112
- */
1113
- extend: function(options) {
1114
- Object.extend(self, options);
1115
- return self;
1116
- },
1117
- /**
1118
- Includes a module in this object.
1119
-
1120
- myObject = Core()
1121
- myObject.include(Bindable)
1122
-
1123
- # now you can bind handlers to functions
1124
- myObject.bind "someEvent", ->
1125
- alert("wow. that was easy.")
1126
-
1127
- @name include
1128
- @methodOf Core#
1129
- @param {String} Module the module to include. A module is a constructor that takes two parameters, I and self, and returns an object containing the public methods to extend the including object with.
1130
- */
1131
- include: function() {
1132
- var Module, key, moduleName, modules, value, _i, _len;
1133
- modules = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
1134
- for (_i = 0, _len = modules.length; _i < _len; _i++) {
1135
- Module = modules[_i];
1136
- if (typeof Module.isString === "function" ? Module.isString() : void 0) {
1137
- moduleName = Module;
1138
- Module = Module.constantize();
1139
- } else if (moduleName = Module._name) {} else {
1140
- for (key in root) {
1141
- value = root[key];
1142
- if (value === Module) Module._name = moduleName = key;
1143
- }
1144
- }
1145
- if (moduleName) {
1146
- if (!I.includedModules.include(moduleName)) {
1147
- I.includedModules.push(moduleName);
1148
- self.extend(Module(I, self));
1149
- }
1150
- } else {
1151
- warn("Unable to discover name for module: ", Module, "\nSerialization issues may occur.");
1152
- self.extend(Module(I, self));
1153
- }
1154
- }
1155
- return self;
1156
- },
1157
- send: function() {
1158
- var args, name;
1159
- name = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
1160
- return self[name].apply(self, args);
1161
- }
1162
- };
1163
- self.include("Bindable");
1164
- _ref = I.includedModules;
1165
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
1166
- moduleName = _ref[_i];
1167
- Module = moduleName.constantize();
1168
- self.extend(Module(I, self));
1169
- }
1170
- return self;
1171
- };
1172
- })();
1173
-
1174
- var __slice = Array.prototype.slice;
1175
-
1176
- Function.prototype.once = function() {
1177
- var func, memo, ran;
1178
- func = this;
1179
- ran = false;
1180
- memo = void 0;
1181
- return function() {
1182
- if (ran) return memo;
1183
- ran = true;
1184
- return memo = func.apply(this, arguments);
1185
- };
1186
- };
1187
-
1188
- /**
1189
- Calling a debounced function will postpone its execution until after
1190
- wait milliseconds have elapsed since the last time the function was
1191
- invoked. Useful for implementing behavior that should only happen after
1192
- the input has stopped arriving. For example: rendering a preview of a
1193
- Markdown comment, recalculating a layout after the window has stopped
1194
- being resized...
1195
-
1196
- lazyLayout = calculateLayout.debounce(300)
1197
- $(window).resize(lazyLayout)
1198
-
1199
- @name debounce
1200
- @methodOf Function#
1201
- @returns {Function} The debounced version of this function.
1202
- */
1203
-
1204
- Function.prototype.debounce = function(wait) {
1205
- var func, timeout;
1206
- timeout = null;
1207
- func = this;
1208
- return function() {
1209
- var args, context, later;
1210
- context = this;
1211
- args = arguments;
1212
- later = function() {
1213
- timeout = null;
1214
- return func.apply(context, args);
1215
- };
1216
- clearTimeout(timeout);
1217
- return timeout = setTimeout(later, wait);
1218
- };
1219
- };
1220
-
1221
- Function.prototype.returning = function(x) {
1222
- var func;
1223
- func = this;
1224
- return function() {
1225
- func.apply(this, arguments);
1226
- return x;
1227
- };
1228
- };
1229
-
1230
- Function.prototype.delay = function() {
1231
- var args, func, wait;
1232
- wait = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
1233
- func = this;
1234
- return setTimeout(function() {
1235
- return func.apply(null, args);
1236
- }, wait);
1237
- };
1238
-
1239
- Function.prototype.defer = function() {
1240
- var args;
1241
- args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
1242
- return this.delay.apply(this, [1].concat(args));
1243
- };
1244
-
1245
- /**
1246
- @name Logging
1247
- @namespace
1248
-
1249
- Gives you some convenience methods for outputting data while developing.
1250
-
1251
- log "Testing123"
1252
- info "Hey, this is happening"
1253
- warn "Be careful, this might be a problem"
1254
- error "Kaboom!"
1255
- */
1256
- var __slice = Array.prototype.slice;
1257
-
1258
- ["log", "info", "warn", "error"].each(function(name) {
1259
- if (typeof console !== "undefined") {
1260
- return (typeof exports !== "undefined" && exports !== null ? exports : this)[name] = function() {
1261
- var args;
1262
- args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
1263
- if (console[name]) return console[name].apply(console, args);
1264
- };
1265
- } else {
1266
- return (typeof exports !== "undefined" && exports !== null ? exports : this)[name] = function() {};
1267
- }
1268
- });
1269
-
1270
- /**
1271
- * Matrix.js v1.3.0pre
1272
- *
1273
- * Copyright (c) 2010 STRd6
1274
- *
1275
- * Permission is hereby granted, free of charge, to any person obtaining a copy
1276
- * of this software and associated documentation files (the "Software"), to deal
1277
- * in the Software without restriction, including without limitation the rights
1278
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1279
- * copies of the Software, and to permit persons to whom the Software is
1280
- * furnished to do so, subject to the following conditions:
1281
- *
1282
- * The above copyright notice and this permission notice shall be included in
1283
- * all copies or substantial portions of the Software.
1284
- *
1285
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1286
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1287
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1288
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1289
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1290
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1291
- * THE SOFTWARE.
1292
- *
1293
- * Loosely based on flash:
1294
- * http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/geom/Matrix.html
1295
- */
1296
- (function() {
1297
- /**
1298
- <pre>
1299
- _ _
1300
- | a c tx |
1301
- | b d ty |
1302
- |_0 0 1 _|
1303
- </pre>
1304
- Creates a matrix for 2d affine transformations.
1305
-
1306
- concat, inverse, rotate, scale and translate return new matrices with the
1307
- transformations applied. The matrix is not modified in place.
1308
-
1309
- Returns the identity matrix when called with no arguments.
1310
-
1311
- @name Matrix
1312
- @param {Number} [a]
1313
- @param {Number} [b]
1314
- @param {Number} [c]
1315
- @param {Number} [d]
1316
- @param {Number} [tx]
1317
- @param {Number} [ty]
1318
- @constructor
1319
- */
1320
- var Matrix;
1321
- Matrix = function(a, b, c, d, tx, ty) {
1322
- var _ref;
1323
- if (Object.isObject(a)) {
1324
- _ref = a, a = _ref.a, b = _ref.b, c = _ref.c, d = _ref.d, tx = _ref.tx, ty = _ref.ty;
1325
- }
1326
- return {
1327
- __proto__: Matrix.prototype,
1328
- /**
1329
- @name a
1330
- @fieldOf Matrix#
1331
- */
1332
- a: a != null ? a : 1,
1333
- /**
1334
- @name b
1335
- @fieldOf Matrix#
1336
- */
1337
- b: b || 0,
1338
- /**
1339
- @name c
1340
- @fieldOf Matrix#
1341
- */
1342
- c: c || 0,
1343
- /**
1344
- @name d
1345
- @fieldOf Matrix#
1346
- */
1347
- d: d != null ? d : 1,
1348
- /**
1349
- @name tx
1350
- @fieldOf Matrix#
1351
- */
1352
- tx: tx || 0,
1353
- /**
1354
- @name ty
1355
- @fieldOf Matrix#
1356
- */
1357
- ty: ty || 0
1358
- };
1359
- };
1360
- Matrix.prototype = {
1361
- /**
1362
- Returns the result of this matrix multiplied by another matrix
1363
- combining the geometric effects of the two. In mathematical terms,
1364
- concatenating two matrixes is the same as combining them using matrix multiplication.
1365
- If this matrix is A and the matrix passed in is B, the resulting matrix is A x B
1366
- http://mathworld.wolfram.com/MatrixMultiplication.html
1367
- @name concat
1368
- @methodOf Matrix#
1369
- @param {Matrix} matrix The matrix to multiply this matrix by.
1370
- @returns {Matrix} The result of the matrix multiplication, a new matrix.
1371
- */
1372
- concat: function(matrix) {
1373
- return Matrix(this.a * matrix.a + this.c * matrix.b, this.b * matrix.a + this.d * matrix.b, this.a * matrix.c + this.c * matrix.d, this.b * matrix.c + this.d * matrix.d, this.a * matrix.tx + this.c * matrix.ty + this.tx, this.b * matrix.tx + this.d * matrix.ty + this.ty);
1374
- },
1375
- /**
1376
- Copy this matrix.
1377
- @name copy
1378
- @methodOf Matrix#
1379
- @returns {Matrix} A copy of this matrix.
1380
- */
1381
- copy: function() {
1382
- return Matrix(this.a, this.b, this.c, this.d, this.tx, this.ty);
1383
- },
1384
- /**
1385
- Given a point in the pretransform coordinate space, returns the coordinates of
1386
- that point after the transformation occurs. Unlike the standard transformation
1387
- applied using the transformPoint() method, the deltaTransformPoint() method
1388
- does not consider the translation parameters tx and ty.
1389
- @name deltaTransformPoint
1390
- @methodOf Matrix#
1391
- @see #transformPoint
1392
- @return {Point} A new point transformed by this matrix ignoring tx and ty.
1393
- */
1394
- deltaTransformPoint: function(point) {
1395
- return Point(this.a * point.x + this.c * point.y, this.b * point.x + this.d * point.y);
1396
- },
1397
- /**
1398
- Returns the inverse of the matrix.
1399
- http://mathworld.wolfram.com/MatrixInverse.html
1400
- @name inverse
1401
- @methodOf Matrix#
1402
- @returns {Matrix} A new matrix that is the inverse of this matrix.
1403
- */
1404
- inverse: function() {
1405
- var determinant;
1406
- determinant = this.a * this.d - this.b * this.c;
1407
- return Matrix(this.d / determinant, -this.b / determinant, -this.c / determinant, this.a / determinant, (this.c * this.ty - this.d * this.tx) / determinant, (this.b * this.tx - this.a * this.ty) / determinant);
1408
- },
1409
- /**
1410
- Returns a new matrix that corresponds this matrix multiplied by a
1411
- a rotation matrix.
1412
- @name rotate
1413
- @methodOf Matrix#
1414
- @see Matrix.rotation
1415
- @param {Number} theta Amount to rotate in radians.
1416
- @param {Point} [aboutPoint] The point about which this rotation occurs. Defaults to (0,0).
1417
- @returns {Matrix} A new matrix, rotated by the specified amount.
1418
- */
1419
- rotate: function(theta, aboutPoint) {
1420
- return this.concat(Matrix.rotation(theta, aboutPoint));
1421
- },
1422
- /**
1423
- Returns a new matrix that corresponds this matrix multiplied by a
1424
- a scaling matrix.
1425
- @name scale
1426
- @methodOf Matrix#
1427
- @see Matrix.scale
1428
- @param {Number} sx
1429
- @param {Number} [sy]
1430
- @param {Point} [aboutPoint] The point that remains fixed during the scaling
1431
- @returns {Matrix} A new Matrix. The original multiplied by a scaling matrix.
1432
- */
1433
- scale: function(sx, sy, aboutPoint) {
1434
- return this.concat(Matrix.scale(sx, sy, aboutPoint));
1435
- },
1436
- /**
1437
- Returns a new matrix that corresponds this matrix multiplied by a
1438
- a skewing matrix.
1439
-
1440
- @name skew
1441
- @methodOf Matrix#
1442
- @see Matrix.skew
1443
- @param {Number} skewX The angle of skew in the x dimension.
1444
- @param {Number} skewY The angle of skew in the y dimension.
1445
- */
1446
- skew: function(skewX, skewY) {
1447
- return this.concat(Matrix.skew(skewX, skewY));
1448
- },
1449
- /**
1450
- Returns a string representation of this matrix.
1451
-
1452
- @name toString
1453
- @methodOf Matrix#
1454
- @returns {String} A string reperesentation of this matrix.
1455
- */
1456
- toString: function() {
1457
- return "Matrix(" + this.a + ", " + this.b + ", " + this.c + ", " + this.d + ", " + this.tx + ", " + this.ty + ")";
1458
- },
1459
- /**
1460
- Returns the result of applying the geometric transformation represented by the
1461
- Matrix object to the specified point.
1462
- @name transformPoint
1463
- @methodOf Matrix#
1464
- @see #deltaTransformPoint
1465
- @returns {Point} A new point with the transformation applied.
1466
- */
1467
- transformPoint: function(point) {
1468
- return Point(this.a * point.x + this.c * point.y + this.tx, this.b * point.x + this.d * point.y + this.ty);
1469
- },
1470
- /**
1471
- Translates the matrix along the x and y axes, as specified by the tx and ty parameters.
1472
- @name translate
1473
- @methodOf Matrix#
1474
- @see Matrix.translation
1475
- @param {Number} tx The translation along the x axis.
1476
- @param {Number} ty The translation along the y axis.
1477
- @returns {Matrix} A new matrix with the translation applied.
1478
- */
1479
- translate: function(tx, ty) {
1480
- return this.concat(Matrix.translation(tx, ty));
1481
- }
1482
- };
1483
- /**
1484
- Creates a matrix transformation that corresponds to the given rotation,
1485
- around (0,0) or the specified point.
1486
- @see Matrix#rotate
1487
- @param {Number} theta Rotation in radians.
1488
- @param {Point} [aboutPoint] The point about which this rotation occurs. Defaults to (0,0).
1489
- @returns {Matrix} A new matrix rotated by the given amount.
1490
- */
1491
- Matrix.rotate = Matrix.rotation = function(theta, aboutPoint) {
1492
- var rotationMatrix;
1493
- rotationMatrix = Matrix(Math.cos(theta), Math.sin(theta), -Math.sin(theta), Math.cos(theta));
1494
- if (aboutPoint != null) {
1495
- rotationMatrix = Matrix.translation(aboutPoint.x, aboutPoint.y).concat(rotationMatrix).concat(Matrix.translation(-aboutPoint.x, -aboutPoint.y));
1496
- }
1497
- return rotationMatrix;
1498
- };
1499
- /**
1500
- Returns a matrix that corresponds to scaling by factors of sx, sy along
1501
- the x and y axis respectively.
1502
- If only one parameter is given the matrix is scaled uniformly along both axis.
1503
- If the optional aboutPoint parameter is given the scaling takes place
1504
- about the given point.
1505
- @see Matrix#scale
1506
- @param {Number} sx The amount to scale by along the x axis or uniformly if no sy is given.
1507
- @param {Number} [sy] The amount to scale by along the y axis.
1508
- @param {Point} [aboutPoint] The point about which the scaling occurs. Defaults to (0,0).
1509
- @returns {Matrix} A matrix transformation representing scaling by sx and sy.
1510
- */
1511
- Matrix.scale = function(sx, sy, aboutPoint) {
1512
- var scaleMatrix;
1513
- sy = sy || sx;
1514
- scaleMatrix = Matrix(sx, 0, 0, sy);
1515
- if (aboutPoint) {
1516
- scaleMatrix = Matrix.translation(aboutPoint.x, aboutPoint.y).concat(scaleMatrix).concat(Matrix.translation(-aboutPoint.x, -aboutPoint.y));
1517
- }
1518
- return scaleMatrix;
1519
- };
1520
- /**
1521
- Returns a matrix that corresponds to a skew of skewX, skewY.
1522
-
1523
- @see Matrix#skew
1524
- @param {Number} skewX The angle of skew in the x dimension.
1525
- @param {Number} skewY The angle of skew in the y dimension.
1526
- @return {Matrix} A matrix transformation representing a skew by skewX and skewY.
1527
- */
1528
- Matrix.skew = function(skewX, skewY) {
1529
- return Matrix(0, Math.tan(skewY), Math.tan(skewX), 0);
1530
- };
1531
- /**
1532
- Returns a matrix that corresponds to a translation of tx, ty.
1533
- @see Matrix#translate
1534
- @param {Number} tx The amount to translate in the x direction.
1535
- @param {Number} ty The amount to translate in the y direction.
1536
- @return {Matrix} A matrix transformation representing a translation by tx and ty.
1537
- */
1538
- Matrix.translate = Matrix.translation = function(tx, ty) {
1539
- return Matrix(1, 0, 0, 1, tx, ty);
1540
- };
1541
- /**
1542
- A constant representing the identity matrix.
1543
- @name IDENTITY
1544
- @fieldOf Matrix
1545
- */
1546
- Matrix.IDENTITY = Matrix();
1547
- /**
1548
- A constant representing the horizontal flip transformation matrix.
1549
- @name HORIZONTAL_FLIP
1550
- @fieldOf Matrix
1551
- */
1552
- Matrix.HORIZONTAL_FLIP = Matrix(-1, 0, 0, 1);
1553
- /**
1554
- A constant representing the vertical flip transformation matrix.
1555
- @name VERTICAL_FLIP
1556
- @fieldOf Matrix
1557
- */
1558
- Matrix.VERTICAL_FLIP = Matrix(1, 0, 0, -1);
1559
- if (Object.freeze) {
1560
- Object.freeze(Matrix.IDENTITY);
1561
- Object.freeze(Matrix.HORIZONTAL_FLIP);
1562
- Object.freeze(Matrix.VERTICAL_FLIP);
1563
- }
1564
- return (typeof exports !== "undefined" && exports !== null ? exports : this)["Matrix"] = Matrix;
1565
- })();
1566
-
1567
- /**
1568
- Returns the absolute value of this number.
1569
-
1570
- (-4).abs()
1571
- # => 4
1572
-
1573
- @name abs
1574
- @methodOf Number#
1575
- @returns {Number} The absolute value of the number.
1576
- */
1577
- Number.prototype.abs = function() {
1578
- return Math.abs(this);
1579
- };
1580
-
1581
- /**
1582
- Returns the mathematical ceiling of this number.
1583
-
1584
- 4.9.ceil()
1585
- # => 5
1586
-
1587
- 4.2.ceil()
1588
- # => 5
1589
-
1590
- (-1.2).ceil()
1591
- # => -1
1592
-
1593
- @name ceil
1594
- @methodOf Number#
1595
- @returns {Number} The number truncated to the nearest integer of greater than or equal value.
1596
- */
1597
-
1598
- Number.prototype.ceil = function() {
1599
- return Math.ceil(this);
1600
- };
1601
-
1602
- /**
1603
- Returns the mathematical floor of this number.
1604
-
1605
- 4.9.floor()
1606
- # => 4
1607
-
1608
- 4.2.floor()
1609
- # => 4
1610
-
1611
- (-1.2).floor()
1612
- # => -2
1613
-
1614
- @name floor
1615
- @methodOf Number#
1616
- @returns {Number} The number truncated to the nearest integer of less than or equal value.
1617
- */
1618
-
1619
- Number.prototype.floor = function() {
1620
- return Math.floor(this);
1621
- };
1622
-
1623
- /**
1624
- Returns this number rounded to the nearest integer.
1625
-
1626
- 4.5.round()
1627
- # => 5
1628
-
1629
- 4.4.round()
1630
- # => 4
1631
-
1632
- @name round
1633
- @methodOf Number#
1634
- @returns {Number} The number rounded to the nearest integer.
1635
- */
1636
-
1637
- Number.prototype.round = function() {
1638
- return Math.round(this);
1639
- };
1640
-
1641
- /**
1642
- Get a bunch of points equally spaced around the unit circle.
1643
-
1644
- 4.circularPoints (p) ->
1645
-
1646
- # p gets Point(1, 0), Point(0, 1), Point(-1, 0), Point(0, -1)
1647
-
1648
- @name circularPoint
1649
- @methodOf Number#
1650
- */
1651
-
1652
- Number.prototype.circularPoints = function(block) {
1653
- var n;
1654
- n = this;
1655
- return n.times(function(i) {
1656
- return block(Point.fromAngle((i / n).turns), i);
1657
- });
1658
- };
1659
-
1660
- /**
1661
- Returns a number whose value is limited to the given range.
1662
-
1663
- # limit the output of this computation to between 0 and 255
1664
- (2 * 255).clamp(0, 255)
1665
- # => 255
1666
-
1667
- @name clamp
1668
- @methodOf Number#
1669
- @param {Number} min The lower boundary of the output range
1670
- @param {Number} max The upper boundary of the output range
1671
- @returns {Number} A number in the range [min, max]
1672
- */
1673
-
1674
- Number.prototype.clamp = function(min, max) {
1675
- if ((min != null) && (max != null)) {
1676
- return Math.min(Math.max(this, min), max);
1677
- } else if (min != null) {
1678
- return Math.max(this, min);
1679
- } else if (max != null) {
1680
- return Math.min(this, max);
1681
- } else {
1682
- return this;
1683
- }
1684
- };
1685
-
1686
- /**
1687
- A mod method useful for array wrapping. The range of the function is
1688
- constrained to remain in bounds of array indices.
1689
-
1690
- (-1).mod(5)
1691
- # => 4
1692
-
1693
- @name mod
1694
- @methodOf Number#
1695
- @param {Number} base
1696
- @returns {Number} An integer between 0 and (base - 1) if base is positive.
1697
- */
1698
-
1699
- Number.prototype.mod = function(base) {
1700
- var result;
1701
- result = this % base;
1702
- if (result < 0 && base > 0) result += base;
1703
- return result;
1704
- };
1705
-
1706
- /**
1707
- Get the sign of this number as an integer (1, -1, or 0).
1708
-
1709
- (-5).sign()
1710
- # => -1
1711
-
1712
- 0.sign()
1713
- # => 0
1714
-
1715
- 5.sign()
1716
- # => 1
1717
-
1718
- @name sign
1719
- @methodOf Number#
1720
- @returns {Number} The sign of this number, 0 if the number is 0.
1721
- */
1722
-
1723
- Number.prototype.sign = function() {
1724
- if (this > 0) {
1725
- return 1;
1726
- } else if (this < 0) {
1727
- return -1;
1728
- } else {
1729
- return 0;
1730
- }
1731
- };
1732
-
1733
- /**
1734
- Returns true if this number is even (evenly divisible by 2).
1735
-
1736
- 2.even()
1737
- # => true
1738
-
1739
- 3.even()
1740
- # => false
1741
-
1742
- 0.even()
1743
- # => true
1744
-
1745
- @name even
1746
- @methodOf Number#
1747
- @returns {Boolean} true if this number is an even integer, false otherwise.
1748
- */
1749
-
1750
- Number.prototype.even = function() {
1751
- return this % 2 === 0;
1752
- };
1753
-
1754
- /**
1755
- Returns true if this number is odd (has remainder of 1 when divided by 2).
1756
-
1757
- 2.odd()
1758
- # => false
1759
-
1760
- 3.odd()
1761
- # => true
1762
-
1763
- 0.odd()
1764
- # => false
1765
-
1766
- @name odd
1767
- @methodOf Number#
1768
- @returns {Boolean} true if this number is an odd integer, false otherwise.
1769
- */
1770
-
1771
- Number.prototype.odd = function() {
1772
- if (this > 0) {
1773
- return this % 2 === 1;
1774
- } else {
1775
- return this % 2 === -1;
1776
- }
1777
- };
1778
-
1779
- /**
1780
- Calls iterator the specified number of times, passing in the number of the
1781
- current iteration as a parameter: 0 on first call, 1 on the second call, etc.
1782
-
1783
- output = []
1784
-
1785
- 5.times (n) ->
1786
- output.push(n)
1787
-
1788
- output
1789
- # => [0, 1, 2, 3, 4]
1790
-
1791
- @name times
1792
- @methodOf Number#
1793
- @param {Function} iterator The iterator takes a single parameter, the number of the current iteration.
1794
- @param {Object} [context] The optional context parameter specifies an object to treat as <code>this</code> in the iterator block.
1795
- @returns {Number} The number of times the iterator was called.
1796
- */
1797
-
1798
- Number.prototype.times = function(iterator, context) {
1799
- var i;
1800
- i = -1;
1801
- while (++i < this) {
1802
- iterator.call(context, i);
1803
- }
1804
- return i;
1805
- };
1806
-
1807
- /**
1808
- Returns the the nearest grid resolution less than or equal to the number.
1809
-
1810
- 7.snap(8)
1811
- # => 0
1812
-
1813
- 4.snap(8)
1814
- # => 0
1815
-
1816
- 12.snap(8)
1817
- # => 8
1818
-
1819
- @name snap
1820
- @methodOf Number#
1821
- @param {Number} resolution The grid resolution to snap to.
1822
- @returns {Number} The nearest multiple of resolution lower than the number.
1823
- */
1824
-
1825
- Number.prototype.snap = function(resolution) {
1826
- var n;
1827
- n = this / resolution;
1828
- 1 / 1;
1829
- return n.floor() * resolution;
1830
- };
1831
-
1832
- /**
1833
- In number theory, integer factorization or prime factorization is the
1834
- breaking down of a composite number into smaller non-trivial divisors,
1835
- which when multiplied together equal the original integer.
1836
-
1837
- Floors the number for purposes of factorization.
1838
-
1839
- 60.primeFactors()
1840
- # => [2, 2, 3, 5]
1841
-
1842
- 37.primeFactors()
1843
- # => [37]
1844
-
1845
- @name primeFactors
1846
- @methodOf Number#
1847
- @returns {Array} An array containing the factorization of this number.
1848
- */
1849
-
1850
- Number.prototype.primeFactors = function() {
1851
- var factors, i, iSquared, n;
1852
- factors = [];
1853
- n = Math.floor(this);
1854
- if (n === 0) return;
1855
- if (n < 0) {
1856
- factors.push(-1);
1857
- n /= -1;
1858
- }
1859
- i = 2;
1860
- iSquared = i * i;
1861
- while (iSquared < n) {
1862
- while ((n % i) === 0) {
1863
- factors.push(i);
1864
- n /= i;
1865
- }
1866
- i += 1;
1867
- iSquared = i * i;
1868
- }
1869
- if (n !== 1) factors.push(n);
1870
- return factors;
1871
- };
1872
-
1873
- /**
1874
- Returns the two character hexidecimal
1875
- representation of numbers 0 through 255.
1876
-
1877
- 255.toColorPart()
1878
- # => "ff"
1879
-
1880
- 0.toColorPart()
1881
- # => "00"
1882
-
1883
- 200.toColorPart()
1884
- # => "c8"
1885
-
1886
- @name toColorPart
1887
- @methodOf Number#
1888
- @returns {String} Hexidecimal representation of the number
1889
- */
1890
-
1891
- Number.prototype.toColorPart = function() {
1892
- var s;
1893
- s = parseInt(this.clamp(0, 255), 10).toString(16);
1894
- if (s.length === 1) s = '0' + s;
1895
- return s;
1896
- };
1897
-
1898
- /**
1899
- Returns a number that is maxDelta closer to target.
1900
-
1901
- 255.approach(0, 5)
1902
- # => 250
1903
-
1904
- 5.approach(0, 10)
1905
- # => 0
1906
-
1907
- @name approach
1908
- @methodOf Number#
1909
- @returns {Number} A number maxDelta toward target
1910
- */
1911
-
1912
- Number.prototype.approach = function(target, maxDelta) {
1913
- return (target - this).clamp(-maxDelta, maxDelta) + this;
1914
- };
1915
-
1916
- /**
1917
- Returns a number that is closer to the target by the ratio.
1918
-
1919
- 255.approachByRatio(0, 0.1)
1920
- # => 229.5
1921
-
1922
- @name approachByRatio
1923
- @methodOf Number#
1924
- @returns {Number} A number toward target by the ratio
1925
- */
1926
-
1927
- Number.prototype.approachByRatio = function(target, ratio) {
1928
- return this.approach(target, this * ratio);
1929
- };
1930
-
1931
- /**
1932
- Returns a number that is closer to the target angle by the delta.
1933
-
1934
- Math.PI.approachRotation(0, Math.PI/4)
1935
- # => 2.356194490192345 # this is (3/4) * Math.PI, which is (1/4) * Math.PI closer to 0 from Math.PI
1936
-
1937
- @name approachRotation
1938
- @methodOf Number#
1939
- @returns {Number} A number toward the target angle by maxDelta
1940
- */
1941
-
1942
- Number.prototype.approachRotation = function(target, maxDelta) {
1943
- while (target > this + Math.PI) {
1944
- target -= Math.TAU;
1945
- }
1946
- while (target < this - Math.PI) {
1947
- target += Math.TAU;
1948
- }
1949
- return (target - this).clamp(-maxDelta, maxDelta) + this;
1950
- };
1951
-
1952
- /**
1953
- Constrains a rotation to between -PI and PI.
1954
-
1955
- (9/4 * Math.PI).constrainRotation()
1956
- # => 0.7853981633974483 # this is (1/4) * Math.PI
1957
-
1958
- @name constrainRotation
1959
- @methodOf Number#
1960
- @returns {Number} This number constrained between -PI and PI.
1961
- */
1962
-
1963
- Number.prototype.constrainRotation = function() {
1964
- var target;
1965
- target = this;
1966
- while (target > Math.PI) {
1967
- target -= Math.TAU;
1968
- }
1969
- while (target < -Math.PI) {
1970
- target += Math.TAU;
1971
- }
1972
- return target;
1973
- };
1974
-
1975
- /**
1976
- The mathematical d operator. Useful for simulating dice rolls.
1977
-
1978
- @name d
1979
- @methodOf Number#
1980
- @returns {Number} The sum of rolling <code>this</code> many <code>sides</code>-sided dice
1981
- */
1982
-
1983
- Number.prototype.d = function(sides) {
1984
- var sum;
1985
- sum = 0;
1986
- this.times(function() {
1987
- return sum += rand(sides) + 1;
1988
- });
1989
- return sum;
1990
- };
1991
-
1992
- /**
1993
- Utility method to convert a number to a duration of seconds.
1994
-
1995
- 3.seconds
1996
- # => 3000
1997
-
1998
- setTimout doSometing, 3.seconds
1999
-
2000
- @name seconds
2001
- @propertyOf Number#
2002
- @returns {Number} This number as a duration of seconds
2003
- */
2004
-
2005
- if (!5..seconds) {
2006
- Object.defineProperty(Number.prototype, 'seconds', {
2007
- get: function() {
2008
- return this * 1000;
2009
- }
2010
- });
2011
- }
2012
-
2013
- if (!1..second) {
2014
- Object.defineProperty(Number.prototype, 'second', {
2015
- get: function() {
2016
- return this * 1000;
2017
- }
2018
- });
2019
- }
2020
-
2021
- /**
2022
- Utility method to convert a number to an amount of rotations.
2023
-
2024
- 0.5.rotations
2025
- # => 3.141592653589793
2026
-
2027
- I.rotation = 0.25.rotations
2028
-
2029
- @name rotations
2030
- @propertyOf Number#
2031
- @returns {Number} This number as an amount of rotations
2032
- */
2033
-
2034
- if (!5..rotations) {
2035
- Object.defineProperty(Number.prototype, 'rotations', {
2036
- get: function() {
2037
- return this * Math.TAU;
2038
- }
2039
- });
2040
- }
2041
-
2042
- if (!1..rotation) {
2043
- Object.defineProperty(Number.prototype, 'rotation', {
2044
- get: function() {
2045
- return this * Math.TAU;
2046
- }
2047
- });
2048
- }
2049
-
2050
- /**
2051
- Utility method to convert a number to an amount of rotations.
2052
-
2053
- 0.5.turns
2054
- # => 3.141592653589793
2055
-
2056
- I.rotation = 0.25.turns
2057
-
2058
- 1.turn # => Math.TAU (aka 2 * Math.PI)
2059
-
2060
- @name turns
2061
- @propertyOf Number#
2062
- @returns {Number} This number as an amount of rotation.
2063
- 1 turn is one complete rotation.
2064
- */
2065
-
2066
- if (!5..turns) {
2067
- Object.defineProperty(Number.prototype, 'turns', {
2068
- get: function() {
2069
- return this * Math.TAU;
2070
- }
2071
- });
2072
- }
2073
-
2074
- if (!1..turn) {
2075
- Object.defineProperty(Number.prototype, 'turn', {
2076
- get: function() {
2077
- return this * Math.TAU;
2078
- }
2079
- });
2080
- }
2081
-
2082
- /**
2083
- Utility method to convert a number to an amount of degrees.
2084
-
2085
- 180.degrees
2086
- # => 3.141592653589793
2087
-
2088
- I.rotation = 90.degrees
2089
-
2090
- @name degrees
2091
- @propertyOf Number#
2092
- @returns {Number} This number as an amount of degrees
2093
- */
2094
-
2095
- if (!2..degrees) {
2096
- Object.defineProperty(Number.prototype, 'degrees', {
2097
- get: function() {
2098
- return this * Math.TAU / 360;
2099
- }
2100
- });
2101
- }
2102
-
2103
- if (!1..degree) {
2104
- Object.defineProperty(Number.prototype, 'degree', {
2105
- get: function() {
2106
- return this * Math.TAU / 360;
2107
- }
2108
- });
2109
- }
2110
-
2111
- /**
2112
- The mathematical circle constant of 1 turn.
2113
-
2114
- @name TAU
2115
- @fieldOf Math
2116
- */
2117
-
2118
- Math.TAU = 2 * Math.PI;
2119
-
2120
- var __slice = Array.prototype.slice;
2121
-
2122
- (function() {
2123
- /**
2124
- Create a new point with given x and y coordinates. If no arguments are given
2125
- defaults to (0, 0).
2126
-
2127
- point = Point()
2128
-
2129
- p.x
2130
- # => 0
2131
-
2132
- p.y
2133
- # => 0
2134
-
2135
- point = Point(-2, 5)
2136
-
2137
- p.x
2138
- # => -2
2139
-
2140
- p.y
2141
- # => 5
2142
-
2143
- @name Point
2144
- @param {Number} [x]
2145
- @param {Number} [y]
2146
- @constructor
2147
- */
2148
- var Point;
2149
- Point = function(x, y) {
2150
- var _ref;
2151
- if (Object.isObject(x)) _ref = x, x = _ref.x, y = _ref.y;
2152
- return {
2153
- __proto__: Point.prototype,
2154
- /**
2155
- The x coordinate of this point.
2156
- @name x
2157
- @fieldOf Point#
2158
- */
2159
- x: x || 0,
2160
- /**
2161
- The y coordinate of this point.
2162
- @name y
2163
- @fieldOf Point#
2164
- */
2165
- y: y || 0
2166
- };
2167
- };
2168
- Point.prototype = {
2169
- /**
2170
- Constrain the magnitude of a vector.
2171
-
2172
- @name clamp
2173
- @methodOf Point#
2174
- @param {Number} n Maximum value for magnitude.
2175
- @returns {Point} A new point whose magnitude has been clamped to the given value.
2176
- */
2177
- clamp: function(n) {
2178
- return this.copy().clamp$(n);
2179
- },
2180
- clamp$: function(n) {
2181
- if (this.magnitude() > n) {
2182
- return this.norm$(n);
2183
- } else {
2184
- return this;
2185
- }
2186
- },
2187
- /**
2188
- Creates a copy of this point.
2189
-
2190
- @name copy
2191
- @methodOf Point#
2192
- @returns {Point} A new point with the same x and y value as this point.
2193
-
2194
- point = Point(1, 1)
2195
- pointCopy = point.copy()
2196
-
2197
- point.equal(pointCopy)
2198
- # => true
2199
-
2200
- point == pointCopy
2201
- # => false
2202
- */
2203
- copy: function() {
2204
- return Point(this.x, this.y);
2205
- },
2206
- /**
2207
- Adds a point to this one and returns the new point. You may
2208
- also use a two argument call like <code>point.add(x, y)</code>
2209
- to add x and y values without a second point object.
2210
-
2211
- point = Point(2, 3).add(Point(3, 4))
2212
-
2213
- point.x
2214
- # => 5
2215
-
2216
- point.y
2217
- # => 7
2218
-
2219
- anotherPoint = Point(2, 3).add(3, 4)
2220
-
2221
- anotherPoint.x
2222
- # => 5
2223
-
2224
- anotherPoint.y
2225
- # => 7
2226
-
2227
- @name add
2228
- @methodOf Point#
2229
- @param {Point} other The point to add this point to.
2230
- @returns {Point} A new point, the sum of both.
2231
- */
2232
- add: function(first, second) {
2233
- return this.copy().add$(first, second);
2234
- },
2235
- /**
2236
- Adds a point to this one, returning a modified point. You may
2237
- also use a two argument call like <code>point.add(x, y)</code>
2238
- to add x and y values without a second point object.
2239
-
2240
- point = Point(2, 3)
2241
-
2242
- point.x
2243
- # => 2
2244
-
2245
- point.y
2246
- # => 3
2247
-
2248
- point.add$(Point(3, 4))
2249
-
2250
- point.x
2251
- # => 5
2252
-
2253
- point.y
2254
- # => 7
2255
-
2256
- anotherPoint = Point(2, 3)
2257
- anotherPoint.add$(3, 4)
2258
-
2259
- anotherPoint.x
2260
- # => 5
2261
-
2262
- anotherPoint.y
2263
- # => 7
2264
-
2265
- @name add$
2266
- @methodOf Point#
2267
- @param {Point} other The point to add this point to.
2268
- @returns {Point} The sum of both points.
2269
- */
2270
- add$: function(first, second) {
2271
- if (second != null) {
2272
- this.x += first;
2273
- this.y += second;
2274
- } else {
2275
- this.x += first.x;
2276
- this.y += first.y;
2277
- }
2278
- return this;
2279
- },
2280
- /**
2281
- Subtracts a point to this one and returns the new point.
2282
-
2283
- point = Point(1, 2).subtract(Point(2, 0))
2284
-
2285
- point.x
2286
- # => -1
2287
-
2288
- point.y
2289
- # => 2
2290
-
2291
- anotherPoint = Point(1, 2).subtract(2, 0)
2292
-
2293
- anotherPoint.x
2294
- # => -1
2295
-
2296
- anotherPoint.y
2297
- # => 2
2298
-
2299
- @name subtract
2300
- @methodOf Point#
2301
- @param {Point} other The point to subtract from this point.
2302
- @returns {Point} A new point, this - other.
2303
- */
2304
- subtract: function(first, second) {
2305
- return this.copy().subtract$(first, second);
2306
- },
2307
- /**
2308
- Subtracts a point to this one and returns the new point.
2309
-
2310
- point = Point(1, 2)
2311
-
2312
- point.x
2313
- # => 1
2314
-
2315
- point.y
2316
- # => 2
2317
-
2318
- point.subtract$(Point(2, 0))
2319
-
2320
- point.x
2321
- # => -1
2322
-
2323
- point.y
2324
- # => 2
2325
-
2326
- anotherPoint = Point(1, 2)
2327
- anotherPoint.subtract$(2, 0)
2328
-
2329
- anotherPoint.x
2330
- # => -1
2331
-
2332
- anotherPoint.y
2333
- # => 2
2334
-
2335
- @name subtract$
2336
- @methodOf Point#
2337
- @param {Point} other The point to subtract from this point.
2338
- @returns {Point} The difference of the two points.
2339
- */
2340
- subtract$: function(first, second) {
2341
- if (second != null) {
2342
- this.x -= first;
2343
- this.y -= second;
2344
- } else {
2345
- this.x -= first.x;
2346
- this.y -= first.y;
2347
- }
2348
- return this;
2349
- },
2350
- /**
2351
- Scale this Point (Vector) by a constant amount.
2352
-
2353
- point = Point(5, 6).scale(2)
2354
-
2355
- point.x
2356
- # => 10
2357
-
2358
- point.y
2359
- # => 12
2360
-
2361
- @name scale
2362
- @methodOf Point#
2363
- @param {Number} scalar The amount to scale this point by.
2364
- @returns {Point} A new point, this * scalar.
2365
- */
2366
- scale: function(scalar) {
2367
- return this.copy().scale$(scalar);
2368
- },
2369
- /**
2370
- Scale this Point (Vector) by a constant amount. Modifies the point in place.
2371
-
2372
- point = Point(5, 6)
2373
-
2374
- point.x
2375
- # => 5
2376
-
2377
- point.y
2378
- # => 6
2379
-
2380
- point.scale$(2)
2381
-
2382
- point.x
2383
- # => 10
2384
-
2385
- point.y
2386
- # => 12
2387
-
2388
- @name scale$
2389
- @methodOf Point#
2390
- @param {Number} scalar The amount to scale this point by.
2391
- @returns {Point} this * scalar.
2392
- */
2393
- scale$: function(scalar) {
2394
- this.x *= scalar;
2395
- this.y *= scalar;
2396
- return this;
2397
- },
2398
- /**
2399
- The norm of a vector is the unit vector pointing in the same direction. This method
2400
- treats the point as though it is a vector from the origin to (x, y).
2401
-
2402
- point = Point(2, 3).norm()
2403
-
2404
- point.x
2405
- # => 0.5547001962252291
2406
-
2407
- point.y
2408
- # => 0.8320502943378437
2409
-
2410
- anotherPoint = Point(2, 3).norm(2)
2411
-
2412
- anotherPoint.x
2413
- # => 1.1094003924504583
2414
-
2415
- anotherPoint.y
2416
- # => 1.6641005886756874
2417
-
2418
- @name norm
2419
- @methodOf Point#
2420
- @returns {Point} The unit vector pointing in the same direction as this vector.
2421
- */
2422
- norm: function(length) {
2423
- if (length == null) length = 1.0;
2424
- return this.copy().norm$(length);
2425
- },
2426
- /**
2427
- The norm of a vector is the unit vector pointing in the same direction. This method
2428
- treats the point as though it is a vector from the origin to (x, y). Modifies the point in place.
2429
-
2430
- point = Point(2, 3).norm$()
2431
-
2432
- point.x
2433
- # => 0.5547001962252291
2434
-
2435
- point.y
2436
- # => 0.8320502943378437
2437
-
2438
- anotherPoint = Point(2, 3).norm$(2)
2439
-
2440
- anotherPoint.x
2441
- # => 1.1094003924504583
2442
-
2443
- anotherPoint.y
2444
- # => 1.6641005886756874
2445
-
2446
- @name norm$
2447
- @methodOf Point#
2448
- @returns {Point} The unit vector pointing in the same direction as this vector.
2449
- */
2450
- norm$: function(length) {
2451
- var m;
2452
- if (length == null) length = 1.0;
2453
- if (m = this.length()) {
2454
- return this.scale$(length / m);
2455
- } else {
2456
- return this;
2457
- }
2458
- },
2459
- /**
2460
- Floor the x and y values, returning a new point.
2461
-
2462
- point = Point(3.4, 5.8).floor()
2463
-
2464
- point.x
2465
- # => 3
2466
-
2467
- point.y
2468
- # => 5
2469
-
2470
- @name floor
2471
- @methodOf Point#
2472
- @returns {Point} A new point, with x and y values each floored to the largest previous integer.
2473
- */
2474
- floor: function() {
2475
- return this.copy().floor$();
2476
- },
2477
- /**
2478
- Floor the x and y values, returning a modified point.
2479
-
2480
- point = Point(3.4, 5.8)
2481
- point.floor$()
2482
-
2483
- point.x
2484
- # => 3
2485
-
2486
- point.y
2487
- # => 5
2488
-
2489
- @name floor$
2490
- @methodOf Point#
2491
- @returns {Point} A modified point, with x and y values each floored to the largest previous integer.
2492
- */
2493
- floor$: function() {
2494
- this.x = this.x.floor();
2495
- this.y = this.y.floor();
2496
- return this;
2497
- },
2498
- /**
2499
- Determine whether this point is equal to another point.
2500
-
2501
- pointA = Point(2, 3)
2502
- pointB = Point(2, 3)
2503
- pointC = Point(4, 5)
2504
-
2505
- pointA.equal(pointB)
2506
- # => true
2507
-
2508
- pointA.equal(pointC)
2509
- # => false
2510
-
2511
- @name equal
2512
- @methodOf Point#
2513
- @param {Point} other The point to check for equality.
2514
- @returns {Boolean} true if the other point has the same x, y coordinates, false otherwise.
2515
- */
2516
- equal: function(other) {
2517
- return this.x === other.x && this.y === other.y;
2518
- },
2519
- /**
2520
- Computed the length of this point as though it were a vector from (0,0) to (x,y).
2521
-
2522
- point = Point(5, 7)
2523
-
2524
- point.length()
2525
- # => 8.602325267042627
2526
-
2527
- @name length
2528
- @methodOf Point#
2529
- @returns {Number} The length of the vector from the origin to this point.
2530
- */
2531
- length: function() {
2532
- return Math.sqrt(this.dot(this));
2533
- },
2534
- /**
2535
- Calculate the magnitude of this Point (Vector).
2536
-
2537
- point = Point(5, 7)
2538
-
2539
- point.magnitude()
2540
- # => 8.602325267042627
2541
-
2542
- @name magnitude
2543
- @methodOf Point#
2544
- @returns {Number} The magnitude of this point as if it were a vector from (0, 0) -> (x, y).
2545
- */
2546
- magnitude: function() {
2547
- return this.length();
2548
- },
2549
- /**
2550
- Returns the direction in radians of this point from the origin.
2551
-
2552
- point = Point(0, 1)
2553
-
2554
- point.direction()
2555
- # => 1.5707963267948966 # Math.PI / 2
2556
-
2557
- @name direction
2558
- @methodOf Point#
2559
- @returns {Number} The direction in radians of this point from the origin
2560
- */
2561
- direction: function() {
2562
- return Math.atan2(this.y, this.x);
2563
- },
2564
- /**
2565
- Calculate the dot product of this point and another point (Vector).
2566
- @name dot
2567
- @methodOf Point#
2568
- @param {Point} other The point to dot with this point.
2569
- @returns {Number} The dot product of this point dot other as a scalar value.
2570
- */
2571
- dot: function(other) {
2572
- return this.x * other.x + this.y * other.y;
2573
- },
2574
- /**
2575
- Calculate the cross product of this point and another point (Vector).
2576
- Usually cross products are thought of as only applying to three dimensional vectors,
2577
- but z can be treated as zero. The result of this method is interpreted as the magnitude
2578
- of the vector result of the cross product between [x1, y1, 0] x [x2, y2, 0]
2579
- perpendicular to the xy plane.
2580
-
2581
- @name cross
2582
- @methodOf Point#
2583
- @param {Point} other The point to cross with this point.
2584
- @returns {Number} The cross product of this point with the other point as scalar value.
2585
- */
2586
- cross: function(other) {
2587
- return this.x * other.y - other.x * this.y;
2588
- },
2589
- /**
2590
- Compute the Euclidean distance between this point and another point.
2591
-
2592
- pointA = Point(2, 3)
2593
- pointB = Point(9, 2)
2594
-
2595
- pointA.distance(pointB)
2596
- # => 7.0710678118654755 # Math.sqrt(50)
2597
-
2598
- @name distance
2599
- @methodOf Point#
2600
- @param {Point} other The point to compute the distance to.
2601
- @returns {Number} The distance between this point and another point.
2602
- */
2603
- distance: function(other) {
2604
- return Point.distance(this, other);
2605
- },
2606
- /**
2607
- @name toString
2608
- @methodOf Point#
2609
- @returns {String} A string representation of this point.
2610
- */
2611
- toString: function() {
2612
- return "Point(" + this.x + ", " + this.y + ")";
2613
- }
2614
- };
2615
- /**
2616
- Compute the Euclidean distance between two points.
2617
-
2618
- pointA = Point(2, 3)
2619
- pointB = Point(9, 2)
2620
-
2621
- Point.distance(pointA, pointB)
2622
- # => 7.0710678118654755 # Math.sqrt(50)
2623
-
2624
- @name distance
2625
- @fieldOf Point
2626
- @param {Point} p1
2627
- @param {Point} p2
2628
- @returns {Number} The Euclidean distance between two points.
2629
- */
2630
- Point.distance = function(p1, p2) {
2631
- return Math.sqrt(Point.distanceSquared(p1, p2));
2632
- };
2633
- /**
2634
- pointA = Point(2, 3)
2635
- pointB = Point(9, 2)
2636
-
2637
- Point.distanceSquared(pointA, pointB)
2638
- # => 50
2639
-
2640
- @name distanceSquared
2641
- @fieldOf Point
2642
- @param {Point} p1
2643
- @param {Point} p2
2644
- @returns {Number} The square of the Euclidean distance between two points.
2645
- */
2646
- Point.distanceSquared = function(p1, p2) {
2647
- return Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2);
2648
- };
2649
- /**
2650
- @name interpolate
2651
- @fieldOf Point
2652
-
2653
- @param {Point} p1
2654
- @param {Point} p2
2655
- @param {Number} t
2656
- @returns {Point} A point along the path from p1 to p2
2657
- */
2658
- Point.interpolate = function(p1, p2, t) {
2659
- return p2.subtract(p1).scale(t).add(p1);
2660
- };
2661
- /**
2662
- Construct a point on the unit circle for the given angle.
2663
-
2664
- point = Point.fromAngle(Math.PI / 2)
2665
-
2666
- point.x
2667
- # => 0
2668
-
2669
- point.y
2670
- # => 1
2671
-
2672
- @name fromAngle
2673
- @fieldOf Point
2674
- @param {Number} angle The angle in radians
2675
- @returns {Point} The point on the unit circle.
2676
- */
2677
- Point.fromAngle = function(angle) {
2678
- return Point(Math.cos(angle), Math.sin(angle));
2679
- };
2680
- /**
2681
- If you have two dudes, one standing at point p1, and the other
2682
- standing at point p2, then this method will return the direction
2683
- that the dude standing at p1 will need to face to look at p2.
2684
-
2685
- p1 = Point(0, 0)
2686
- p2 = Point(7, 3)
2687
-
2688
- Point.direction(p1, p2)
2689
- # => 0.40489178628508343
2690
-
2691
- @name direction
2692
- @fieldOf Point
2693
- @param {Point} p1 The starting point.
2694
- @param {Point} p2 The ending point.
2695
- @returns {Number} The direction from p1 to p2 in radians.
2696
- */
2697
- Point.direction = function(p1, p2) {
2698
- return Math.atan2(p2.y - p1.y, p2.x - p1.x);
2699
- };
2700
- /**
2701
- The centroid of a set of points is their arithmetic mean.
2702
-
2703
- @name centroid
2704
- @methodOf Point
2705
- @param points... The points to find the centroid of.
2706
- */
2707
- Point.centroid = function() {
2708
- var points;
2709
- points = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
2710
- return points.inject(Point(0, 0), function(sumPoint, point) {
2711
- return sumPoint.add(point);
2712
- }).scale(1 / points.length);
2713
- };
2714
- /**
2715
- Generate a random point on the unit circle.
2716
-
2717
- @returns {Point} A random point on the unit circle.
2718
- */
2719
- Point.random = function() {
2720
- return Point.fromAngle(Random.angle());
2721
- };
2722
- /**
2723
- @name ZERO
2724
- @fieldOf Point
2725
- @returns {Point} The point (0, 0)
2726
- */
2727
- Point.ZERO = Point(0, 0);
2728
- /**
2729
- @name LEFT
2730
- @fieldOf Point
2731
- @returns {Point} The point (-1, 0)
2732
- */
2733
- Point.LEFT = Point(-1, 0);
2734
- /**
2735
- @name RIGHT
2736
- @fieldOf Point
2737
- @returns {Point} The point (1, 0)
2738
- */
2739
- Point.RIGHT = Point(1, 0);
2740
- /**
2741
- @name UP
2742
- @fieldOf Point
2743
- @returns {Point} The point (0, -1)
2744
- */
2745
- Point.UP = Point(0, -1);
2746
- /**
2747
- @name DOWN
2748
- @fieldOf Point
2749
- @returns {Point} The point (0, 1)
2750
- */
2751
- Point.DOWN = Point(0, 1);
2752
- if (Object.freeze) {
2753
- Object.freeze(Point.ZERO);
2754
- Object.freeze(Point.LEFT);
2755
- Object.freeze(Point.RIGHT);
2756
- Object.freeze(Point.UP);
2757
- Object.freeze(Point.DOWN);
2758
- }
2759
- return (typeof exports !== "undefined" && exports !== null ? exports : this)["Point"] = Point;
2760
- })();
2761
-
2762
-
2763
- (function() {
2764
- /**
2765
- @name Random
2766
- @namespace Some useful methods for generating random things.
2767
- */ (typeof exports !== "undefined" && exports !== null ? exports : this)["Random"] = {
2768
- /**
2769
- Returns a random angle, uniformly distributed, between 0 and 2pi.
2770
-
2771
- @name angle
2772
- @methodOf Random
2773
- @returns {Number} A random angle between 0 and 2pi
2774
- */
2775
- angle: function() {
2776
- return rand() * Math.TAU;
2777
- },
2778
- /**
2779
- Returns a random angle between the given angles.
2780
-
2781
- @name angleBetween
2782
- @methodOf Random
2783
- @returns {Number} A random angle between the angles given.
2784
- */
2785
- angleBetween: function(min, max) {
2786
- return rand() * (max - min) + min;
2787
- },
2788
- /**
2789
- Returns a random color.
2790
-
2791
- @name color
2792
- @methodOf Random
2793
- @returns {Color} A random color
2794
- */
2795
- color: function() {
2796
- return Color.random();
2797
- },
2798
- /**
2799
- Happens often.
2800
-
2801
- @name often
2802
- @methodOf Random
2803
- */
2804
- often: function() {
2805
- return rand(3);
2806
- },
2807
- /**
2808
- Happens sometimes.
2809
-
2810
- @name sometimes
2811
- @methodOf Random
2812
- */
2813
- sometimes: function() {
2814
- return !rand(3);
2815
- }
2816
- };
2817
- /**
2818
- Returns random integers from [0, n) if n is given.
2819
- Otherwise returns random float between 0 and 1.
2820
-
2821
- @name rand
2822
- @methodOf window
2823
- @param {Number} n
2824
- @returns {Number} A random integer from 0 to n - 1 if n is given. If n is not given, a random float between 0 and 1.
2825
- */
2826
- (typeof exports !== "undefined" && exports !== null ? exports : this)["rand"] = function(n) {
2827
- if (n) {
2828
- return Math.floor(n * Math.random());
2829
- } else {
2830
- return Math.random();
2831
- }
2832
- };
2833
- /**
2834
- Returns random float from [-n / 2, n / 2] if n is given.
2835
- Otherwise returns random float between -0.5 and 0.5.
2836
-
2837
- @name signedRand
2838
- @methodOf window
2839
- @param {Number} n
2840
- @returns {Number} A random float from -n / 2 to n / 2 if n is given. If n is not given, a random float between -0.5 and 0.5.
2841
- */
2842
- return (typeof exports !== "undefined" && exports !== null ? exports : this)["signedRand"] = function(n) {
2843
- if (n) {
2844
- return (n * Math.random()) - (n / 2);
2845
- } else {
2846
- return Math.random() - 0.5;
2847
- }
2848
- };
2849
- })();
2850
-
2851
-
2852
- (function() {
2853
- var Rectangle;
2854
- Rectangle = function(_arg) {
2855
- var height, width, x, y;
2856
- x = _arg.x, y = _arg.y, width = _arg.width, height = _arg.height;
2857
- return {
2858
- __proto__: Rectangle.prototype,
2859
- x: x || 0,
2860
- y: y || 0,
2861
- width: width || 0,
2862
- height: height || 0
2863
- };
2864
- };
2865
- Rectangle.prototype = {
2866
- center: function() {
2867
- return Point(this.x + this.width / 2, this.y + this.height / 2);
2868
- },
2869
- equal: function(other) {
2870
- return this.x === other.x && this.y === other.y && this.width === other.width && this.height === other.height;
2871
- }
2872
- };
2873
- Rectangle.prototype.__defineGetter__('left', function() {
2874
- return this.x;
2875
- });
2876
- Rectangle.prototype.__defineGetter__('right', function() {
2877
- return this.x + this.width;
2878
- });
2879
- Rectangle.prototype.__defineGetter__('top', function() {
2880
- return this.y;
2881
- });
2882
- Rectangle.prototype.__defineGetter__('bottom', function() {
2883
- return this.y + this.height;
2884
- });
2885
- return (typeof exports !== "undefined" && exports !== null ? exports : this)["Rectangle"] = Rectangle;
2886
- })();
2887
-
2888
- /**
2889
- Returns true if this string only contains whitespace characters.
2890
-
2891
- "".blank()
2892
- # => true
2893
-
2894
- "hello".blank()
2895
- # => false
2896
-
2897
- " ".blank()
2898
- # => true
2899
-
2900
- @name blank
2901
- @methodOf String#
2902
- @returns {Boolean} Whether or not this string is blank.
2903
- */
2904
- String.prototype.blank = function() {
2905
- return /^\s*$/.test(this);
2906
- };
2907
-
2908
- /**
2909
- Returns a new string that is a camelCase version.
2910
-
2911
- "camel_case".camelize()
2912
- "camel-case".camelize()
2913
- "camel case".camelize()
2914
-
2915
- # => "camelCase"
2916
-
2917
- @name camelize
2918
- @methodOf String#
2919
- @returns {String} A new string. camelCase version of `this`.
2920
- */
2921
-
2922
- String.prototype.camelize = function() {
2923
- return this.trim().replace(/(\-|_|\s)+(.)?/g, function(match, separator, chr) {
2924
- if (chr) {
2925
- return chr.toUpperCase();
2926
- } else {
2927
- return '';
2928
- }
2929
- });
2930
- };
2931
-
2932
- /**
2933
- Returns a new string with the first letter capitalized and the rest lower cased.
2934
-
2935
- "capital".capitalize()
2936
- "cAPITAL".capitalize()
2937
- "cApItAl".capitalize()
2938
- "CAPITAL".capitalize()
2939
-
2940
- # => "Capital"
2941
-
2942
- @name capitalize
2943
- @methodOf String#
2944
- @returns {String} A new string. Capitalized version of `this`
2945
- */
2946
-
2947
- String.prototype.capitalize = function() {
2948
- return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase();
2949
- };
2950
-
2951
- /**
2952
- Return the class or constant named in this string.
2953
-
2954
-
2955
- "Constant".constantize()
2956
- # => Constant
2957
- # notice this isn't a string. Useful for calling methods on class with the same name as `this`.
2958
-
2959
- @name constantize
2960
- @methodOf String#
2961
- @returns {Object} The class or constant named in this string.
2962
- */
2963
-
2964
- String.prototype.constantize = function() {
2965
- var item, target, _i, _len, _ref;
2966
- target = typeof exports !== "undefined" && exports !== null ? exports : window;
2967
- _ref = this.split('.');
2968
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
2969
- item = _ref[_i];
2970
- target = target[item];
2971
- }
2972
- return target;
2973
- };
2974
-
2975
- /**
2976
- Get the file extension of a string.
2977
-
2978
- "README.md".extension() # => "md"
2979
- "README".extension() # => ""
2980
-
2981
- @name extension
2982
- @methodOf String#
2983
- @returns {String} File extension
2984
- */
2985
-
2986
- String.prototype.extension = function() {
2987
- var extension, _ref;
2988
- if (extension = (_ref = this.match(/\.([^\.]*)$/, '')) != null ? _ref.last() : void 0) {
2989
- return extension;
2990
- } else {
2991
- return '';
2992
- }
2993
- };
2994
-
2995
- /**
2996
- Returns a new string that is a more human readable version.
2997
-
2998
- "player_id".humanize()
2999
- # => "Player"
3000
-
3001
- "player_ammo".humanize()
3002
- # => "Player ammo"
3003
-
3004
- @name humanize
3005
- @methodOf String#
3006
- @returns {String} A new string. Replaces _id and _ with "" and capitalizes the word.
3007
- */
3008
-
3009
- String.prototype.humanize = function() {
3010
- return this.replace(/_id$/, "").replace(/_/g, " ").capitalize();
3011
- };
3012
-
3013
- /**
3014
- Returns true.
3015
-
3016
- @name isString
3017
- @methodOf String#
3018
- @returns {Boolean} true
3019
- */
3020
-
3021
- String.prototype.isString = function() {
3022
- return true;
3023
- };
3024
-
3025
- /**
3026
- Parse this string as though it is JSON and return the object it represents. If it
3027
- is not valid JSON returns the string itself.
3028
-
3029
- # this is valid json, so an object is returned
3030
- '{"a": 3}'.parse()
3031
- # => {a: 3}
3032
-
3033
- # double quoting instead isn't valid JSON so a string is returned
3034
- "{'a': 3}".parse()
3035
- # => "{'a': 3}"
3036
-
3037
-
3038
- @name parse
3039
- @methodOf String#
3040
- @returns {Object} Returns an object from the JSON this string contains. If it is not valid JSON returns the string itself.
3041
- */
3042
-
3043
- String.prototype.parse = function() {
3044
- try {
3045
- return JSON.parse(this.toString());
3046
- } catch (e) {
3047
- return this.toString();
3048
- }
3049
- };
3050
-
3051
- /**
3052
- Returns true if this string starts with the given string.
3053
-
3054
- @name startsWith
3055
- @methodOf String#
3056
- @param {String} str The string to check.
3057
-
3058
- @returns {Boolean} True if this string starts with the given string, false otherwise.
3059
- */
3060
-
3061
- String.prototype.startsWith = function(str) {
3062
- return this.lastIndexOf(str, 0) === 0;
3063
- };
3064
-
3065
- /**
3066
- Returns a new string in Title Case.
3067
-
3068
- "title-case".titleize()
3069
- # => "Title Case"
3070
-
3071
- "title case".titleize()
3072
- # => "Title Case"
3073
-
3074
- @name titleize
3075
- @methodOf String#
3076
- @returns {String} A new string. Title Cased.
3077
- */
3078
-
3079
- String.prototype.titleize = function() {
3080
- return this.split(/[- ]/).map(function(word) {
3081
- return word.capitalize();
3082
- }).join(' ');
3083
- };
3084
-
3085
- /**
3086
- Underscore a word, changing camelCased with under_scored.
3087
-
3088
- "UNDERScore".underscore()
3089
- # => "under_score"
3090
-
3091
- "UNDER-SCORE".underscore()
3092
- # => "under_score"
3093
-
3094
- "UnDEr-SCorE".underscore()
3095
- # => "un_d_er_s_cor_e"
3096
-
3097
- @name underscore
3098
- @methodOf String#
3099
- @returns {String} A new string. Separated by _.
3100
- */
3101
-
3102
- String.prototype.underscore = function() {
3103
- return this.replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2').replace(/([a-z\d])([A-Z])/g, '$1_$2').replace(/-/g, '_').toLowerCase();
3104
- };
3105
-
3106
- /**
3107
- Assumes the string is something like a file name and returns the
3108
- contents of the string without the extension.
3109
-
3110
- "neat.png".witouthExtension()
3111
- # => "neat"
3112
-
3113
- @name withoutExtension
3114
- @methodOf String#
3115
- @returns {String} A new string without the extension name.
3116
- */
3117
-
3118
- String.prototype.withoutExtension = function() {
3119
- return this.replace(/\.[^\.]*$/, '');
3120
- };
3121
-
3122
- String.prototype.parseHex = function() {
3123
- var alpha, hexString, i, rgb;
3124
- hexString = this.replace(/#/, '');
3125
- switch (hexString.length) {
3126
- case 3:
3127
- case 4:
3128
- if (hexString.length === 4) {
3129
- alpha = (parseInt(hexString.substr(3, 1), 16) * 0x11) / 255;
3130
- } else {
3131
- alpha = 1;
3132
- }
3133
- rgb = (function() {
3134
- var _results;
3135
- _results = [];
3136
- for (i = 0; i <= 2; i++) {
3137
- _results.push(parseInt(hexString.substr(i, 1), 16) * 0x11);
3138
- }
3139
- return _results;
3140
- })();
3141
- rgb.push(alpha);
3142
- return rgb;
3143
- case 6:
3144
- case 8:
3145
- if (hexString.length === 8) {
3146
- alpha = parseInt(hexString.substr(6, 2), 16) / 255;
3147
- } else {
3148
- alpha = 1;
3149
- }
3150
- rgb = (function() {
3151
- var _results;
3152
- _results = [];
3153
- for (i = 0; i <= 2; i++) {
3154
- _results.push(parseInt(hexString.substr(2 * i, 2), 16));
3155
- }
3156
- return _results;
3157
- })();
3158
- rgb.push(alpha);
3159
- return rgb;
3160
- }
3161
- };
3162
-
3163
- /**
3164
- Returns a string representing the specified Boolean object.
3165
-
3166
- <code><em>bool</em>.toString()</code>
3167
-
3168
- @name toString
3169
- @methodOf Boolean#
3170
- */
3171
- /**
3172
- Returns the primitive value of a Boolean object.
3173
-
3174
- <code><em>bool</em>.valueOf()</code>
3175
-
3176
- @name valueOf
3177
- @methodOf Boolean#
3178
- */
3179
- /**
3180
- Returns a string representing the Number object in exponential notation
3181
-
3182
- <code><i>number</i>.toExponential( [<em>fractionDigits</em>] )</code>
3183
- @param fractionDigits
3184
- An integer specifying the number of digits after the decimal point. Defaults
3185
- to as many digits as necessary to specify the number.
3186
- @name toExponential
3187
- @methodOf Number#
3188
- */
3189
- /**
3190
- Formats a number using fixed-point notation
3191
-
3192
- <code><i>number</i>.toFixed( [<em>digits</em>] )</code>
3193
- @param digits The number of digits to appear after the decimal point; this
3194
- may be a value between 0 and 20, inclusive, and implementations may optionally
3195
- support a larger range of values. If this argument is omitted, it is treated as
3196
- 0.
3197
- @name toFixed
3198
- @methodOf Number#
3199
- */
3200
- /**
3201
- number.toLocaleString();
3202
-
3203
- @name toLocaleString
3204
- @methodOf Number#
3205
- */
3206
- /**
3207
- Returns a string representing the Number object to the specified precision.
3208
-
3209
- <code><em>number</em>.toPrecision( [ <em>precision</em> ] )</code>
3210
- @param precision An integer specifying the number of significant digits.
3211
- @name toPrecision
3212
- @methodOf Number#
3213
- */
3214
- /**
3215
- Returns a string representing the specified Number object
3216
-
3217
- <code><i>number</i>.toString( [<em>radix</em>] )</code>
3218
- @param radix
3219
- An integer between 2 and 36 specifying the base to use for representing
3220
- numeric values.
3221
- @name toString
3222
- @methodOf Number#
3223
- */
3224
- /**
3225
- Returns the primitive value of a Number object.
3226
-
3227
- @name valueOf
3228
- @methodOf Number#
3229
- */
3230
- /**
3231
- Returns the specified character from a string.
3232
-
3233
- <code><em>string</em>.charAt(<em>index</em>)</code>
3234
- @param index An integer between 0 and 1 less than the length of the string.
3235
- @name charAt
3236
- @methodOf String#
3237
- */
3238
- /**
3239
- Returns the numeric Unicode value of the character at the given index (except
3240
- for unicode codepoints > 0x10000).
3241
-
3242
-
3243
- @param index An integer greater than 0 and less than the length of the string;
3244
- if it is not a number, it defaults to 0.
3245
- @name charCodeAt
3246
- @methodOf String#
3247
- */
3248
- /**
3249
- Combines the text of two or more strings and returns a new string.
3250
-
3251
- <code><em>string</em>.concat(<em>string2</em>, <em>string3</em>[, ..., <em>stringN</em>])</code>
3252
- @param string2...stringN Strings to concatenate to this string.
3253
- @name concat
3254
- @methodOf String#
3255
- */
3256
- /**
3257
- Returns the index within the calling String object of the first occurrence of
3258
- the specified value, starting the search at fromIndex,
3259
- returns -1 if the value is not found.
3260
-
3261
- <code><em>string</em>.indexOf(<em>searchValue</em>[, <em>fromIndex</em>]</code>
3262
- @param searchValue A string representing the value to search for.
3263
- @param fromIndex The location within the calling string to start the search
3264
- from. It can be any integer between 0 and the length of the string. The default
3265
- value is 0.
3266
- @name indexOf
3267
- @methodOf String#
3268
- */
3269
- /**
3270
- Returns the index within the calling String object of the last occurrence of the
3271
- specified value, or -1 if not found. The calling string is searched backward,
3272
- starting at fromIndex.
3273
-
3274
- <code><em>string</em>.lastIndexOf(<em>searchValue</em>[, <em>fromIndex</em>])</code>
3275
- @param searchValue A string representing the value to search for.
3276
- @param fromIndex The location within the calling string to start the search
3277
- from, indexed from left to right. It can be any integer between 0 and the length
3278
- of the string. The default value is the length of the string.
3279
- @name lastIndexOf
3280
- @methodOf String#
3281
- */
3282
- /**
3283
- Returns a number indicating whether a reference string comes before or after or
3284
- is the same as the given string in sort order.
3285
-
3286
- <code> localeCompare(compareString) </code>
3287
-
3288
- @name localeCompare
3289
- @methodOf String#
3290
- */
3291
- /**
3292
- Used to retrieve the matches when matching a string against a regular
3293
- expression.
3294
-
3295
- <code><em>string</em>.match(<em>regexp</em>)</code>
3296
- @param regexp A regular expression object. If a non-RegExp object obj is passed,
3297
- it is implicitly converted to a RegExp by using new RegExp(obj).
3298
- @name match
3299
- @methodOf String#
3300
- */
3301
- /**
3302
- Returns a new string with some or all matches of a pattern replaced by a
3303
- replacement. The pattern can be a string or a RegExp, and the replacement can
3304
- be a string or a function to be called for each match.
3305
-
3306
- <code><em>str</em>.replace(<em>regexp|substr</em>, <em>newSubStr|function[</em>, </code><code><em>flags]</em>);</code>
3307
- @param regexp A RegExp object. The match is replaced by the return value of
3308
- parameter #2.
3309
- @param substr A String that is to be replaced by newSubStr.
3310
- @param newSubStr The String that replaces the substring received from parameter
3311
- #1. A number of special replacement patterns are supported; see the "Specifying
3312
- a string as a parameter" section below.
3313
- @param function A function to be invoked to create the new substring (to put in
3314
- place of the substring received from parameter #1). The arguments supplied to
3315
- this function are described in the "Specifying a function as a parameter"
3316
- section below.
3317
- @param flags gimy
3318
-
3319
- Non-standardThe use of the flags parameter in the String.replace method is
3320
- non-standard. For cross-browser compatibility, use a RegExp object with
3321
- corresponding flags.A string containing any combination of the RegExp flags: g
3322
- global match i ignore case m match over multiple lines y Non-standard
3323
- sticky global matchignore casematch over multiple linesNon-standard sticky
3324
- @name replace
3325
- @methodOf String#
3326
- */
3327
- /**
3328
- Executes the search for a match between a regular expression and this String
3329
- object.
3330
-
3331
- <code><em>string</em>.search(<em>regexp</em>)</code>
3332
- @param regexp A regular expression object. If a non-RegExp object obj is
3333
- passed, it is implicitly converted to a RegExp by using new RegExp(obj).
3334
- @name search
3335
- @methodOf String#
3336
- */
3337
- /**
3338
- Extracts a section of a string and returns a new string.
3339
-
3340
- <code><em>string</em>.slice(<em>beginslice</em>[, <em>endSlice</em>])</code>
3341
- @param beginSlice The zero-based index at which to begin extraction.
3342
- @param endSlice The zero-based index at which to end extraction. If omitted,
3343
- slice extracts to the end of the string.
3344
- @name slice
3345
- @methodOf String#
3346
- */
3347
- /**
3348
- Splits a String object into an array of strings by separating the string into
3349
- substrings.
3350
-
3351
- <code><em>string</em>.split([<em>separator</em>][, <em>limit</em>])</code>
3352
- @param separator Specifies the character to use for separating the string. The
3353
- separator is treated as a string or a regular expression. If separator is
3354
- omitted, the array returned contains one element consisting of the entire
3355
- string.
3356
- @param limit Integer specifying a limit on the number of splits to be found.
3357
- @name split
3358
- @methodOf String#
3359
- */
3360
- /**
3361
- Returns the characters in a string beginning at the specified location through
3362
- the specified number of characters.
3363
-
3364
- <code><em>string</em>.substr(<em>start</em>[, <em>length</em>])</code>
3365
- @param start Location at which to begin extracting characters.
3366
- @param length The number of characters to extract.
3367
- @name substr
3368
- @methodOf String#
3369
- */
3370
- /**
3371
- Returns a subset of a string between one index and another, or through the end
3372
- of the string.
3373
-
3374
- <code><em>string</em>.substring(<em>indexA</em>[, <em>indexB</em>])</code>
3375
- @param indexA An integer between 0 and one less than the length of the string.
3376
- @param indexB (optional) An integer between 0 and the length of the string.
3377
- @name substring
3378
- @methodOf String#
3379
- */
3380
- /**
3381
- Returns the calling string value converted to lower case, according to any
3382
- locale-specific case mappings.
3383
-
3384
- <code> toLocaleLowerCase() </code>
3385
-
3386
- @name toLocaleLowerCase
3387
- @methodOf String#
3388
- */
3389
- /**
3390
- Returns the calling string value converted to upper case, according to any
3391
- locale-specific case mappings.
3392
-
3393
- <code> toLocaleUpperCase() </code>
3394
-
3395
- @name toLocaleUpperCase
3396
- @methodOf String#
3397
- */
3398
- /**
3399
- Returns the calling string value converted to lowercase.
3400
-
3401
- <code><em>string</em>.toLowerCase()</code>
3402
-
3403
- @name toLowerCase
3404
- @methodOf String#
3405
- */
3406
- /**
3407
- Returns a string representing the specified object.
3408
-
3409
- <code><em>string</em>.toString()</code>
3410
-
3411
- @name toString
3412
- @methodOf String#
3413
- */
3414
- /**
3415
- Returns the calling string value converted to uppercase.
3416
-
3417
- <code><em>string</em>.toUpperCase()</code>
3418
-
3419
- @name toUpperCase
3420
- @methodOf String#
3421
- */
3422
- /**
3423
- Removes whitespace from both ends of the string.
3424
-
3425
- <code><em>string</em>.trim()</code>
3426
-
3427
- @name trim
3428
- @methodOf String#
3429
- */
3430
- /**
3431
- Returns the primitive value of a String object.
3432
-
3433
- <code><em>string</em>.valueOf()</code>
3434
-
3435
- @name valueOf
3436
- @methodOf String#
3437
- */
3438
- /**
3439
- Removes the last element from an array and returns that element.
3440
-
3441
- <code>
3442
- <i>array</i>.pop()
3443
- </code>
3444
-
3445
- @name pop
3446
- @methodOf Array#
3447
- */
3448
- /**
3449
- Mutates an array by appending the given elements and returning the new length of
3450
- the array.
3451
-
3452
- <code><em>array</em>.push(<em>element1</em>, ..., <em>elementN</em>)</code>
3453
- @param element1, ..., elementN The elements to add to the end of the array.
3454
- @name push
3455
- @methodOf Array#
3456
- */
3457
- /**
3458
- Reverses an array in place. The first array element becomes the last and the
3459
- last becomes the first.
3460
-
3461
- <code><em>array</em>.reverse()</code>
3462
-
3463
- @name reverse
3464
- @methodOf Array#
3465
- */
3466
- /**
3467
- Removes the first element from an array and returns that element. This method
3468
- changes the length of the array.
3469
-
3470
- <code><em>array</em>.shift()</code>
3471
-
3472
- @name shift
3473
- @methodOf Array#
3474
- */
3475
- /**
3476
- Sorts the elements of an array in place.
3477
-
3478
- <code><em>array</em>.sort([<em>compareFunction</em>])</code>
3479
- @param compareFunction Specifies a function that defines the sort order. If
3480
- omitted, the array is sorted lexicographically (in dictionary order) according
3481
- to the string conversion of each element.
3482
- @name sort
3483
- @methodOf Array#
3484
- */
3485
- /**
3486
- Changes the content of an array, adding new elements while removing old
3487
- elements.
3488
-
3489
- <code><em>array</em>.splice(<em>index</em>, <em>howMany</em>[, <em>element1</em>[, ...[, <em>elementN</em>]]])</code>
3490
- @param index Index at which to start changing the array. If negative, will
3491
- begin that many elements from the end.
3492
- @param howMany An integer indicating the number of old array elements to
3493
- remove. If howMany is 0, no elements are removed. In this case, you should
3494
- specify at least one new element. If no howMany parameter is specified (second
3495
- syntax above, which is a SpiderMonkey extension), all elements after index are
3496
- removed.
3497
- @param element1, ..., elementN The elements to add to the array. If you don't
3498
- specify any elements, splice simply removes elements from the array.
3499
- @name splice
3500
- @methodOf Array#
3501
- */
3502
- /**
3503
- Adds one or more elements to the beginning of an array and returns the new
3504
- length of the array.
3505
-
3506
- <code><em>arrayName</em>.unshift(<em>element1</em>, ..., <em>elementN</em>) </code>
3507
- @param element1, ..., elementN The elements to add to the front of the array.
3508
- @name unshift
3509
- @methodOf Array#
3510
- */
3511
- /**
3512
- Returns a new array comprised of this array joined with other array(s) and/or
3513
- value(s).
3514
-
3515
- <code><em>array</em>.concat(<em>value1</em>, <em>value2</em>, ..., <em>valueN</em>)</code>
3516
- @param valueN Arrays and/or values to concatenate to the resulting array.
3517
- @name concat
3518
- @methodOf Array#
3519
- */
3520
- /**
3521
- Joins all elements of an array into a string.
3522
-
3523
- <code><em>array</em>.join(<em>separator</em>)</code>
3524
- @param separator Specifies a string to separate each element of the array. The
3525
- separator is converted to a string if necessary. If omitted, the array elements
3526
- are separated with a comma.
3527
- @name join
3528
- @methodOf Array#
3529
- */
3530
- /**
3531
- Returns a one-level deep copy of a portion of an array.
3532
-
3533
- <code><em>array</em>.slice(<em>begin</em>[, <em>end</em>])</code>
3534
- @param begin Zero-based index at which to begin extraction.As a negative index,
3535
- start indicates an offset from the end of the sequence. slice(-2) extracts the
3536
- second-to-last element and the last element in the sequence.
3537
- @param end Zero-based index at which to end extraction. slice extracts up to
3538
- but not including end.slice(1,4) extracts the second element through the fourth
3539
- element (elements indexed 1, 2, and 3).As a negative index, end indicates an
3540
- offset from the end of the sequence. slice(2,-1) extracts the third element
3541
- through the second-to-last element in the sequence.If end is omitted, slice
3542
- extracts to the end of the sequence.
3543
- @name slice
3544
- @methodOf Array#
3545
- */
3546
- /**
3547
- Returns a string representing the specified array and its elements.
3548
-
3549
- <code><em>array</em>.toString()</code>
3550
-
3551
- @name toString
3552
- @methodOf Array#
3553
- */
3554
- /**
3555
- Returns the first index at which a given element can be found in the array, or
3556
- -1 if it is not present.
3557
-
3558
- <code><em>array</em>.indexOf(<em>searchElement</em>[, <em>fromIndex</em>])</code>
3559
- @param searchElement fromIndex Element to locate in the array.The index at
3560
- which to begin the search. Defaults to 0, i.e. the whole array will be searched.
3561
- If the index is greater than or equal to the length of the array, -1 is
3562
- returned, i.e. the array will not be searched. If negative, it is taken as the
3563
- offset from the end of the array. Note that even when the index is negative, the
3564
- array is still searched from front to back. If the calculated index is less than
3565
- 0, the whole array will be searched.
3566
- @name indexOf
3567
- @methodOf Array#
3568
- */
3569
- /**
3570
- Returns the last index at which a given element can be found in the array, or -1
3571
- if it is not present. The array is searched backwards, starting at fromIndex.
3572
-
3573
- <code><em>array</em>.lastIndexOf(<em>searchElement</em>[, <em>fromIndex</em>])</code>
3574
- @param searchElement fromIndex Element to locate in the array.The index at
3575
- which to start searching backwards. Defaults to the array's length, i.e. the
3576
- whole array will be searched. If the index is greater than or equal to the
3577
- length of the array, the whole array will be searched. If negative, it is taken
3578
- as the offset from the end of the array. Note that even when the index is
3579
- negative, the array is still searched from back to front. If the calculated
3580
- index is less than 0, -1 is returned, i.e. the array will not be searched.
3581
- @name lastIndexOf
3582
- @methodOf Array#
3583
- */
3584
- /**
3585
- Creates a new array with all elements that pass the test implemented by the
3586
- provided function.
3587
-
3588
- <code><em>array</em>.filter(<em>callback</em>[, <em>thisObject</em>])</code>
3589
- @param callback thisObject Function to test each element of the array.Object to
3590
- use as this when executing callback.
3591
- @name filter
3592
- @methodOf Array#
3593
- */
3594
- /**
3595
- Executes a provided function once per array element.
3596
-
3597
- <code><em>array</em>.forEach(<em>callback</em>[, <em>thisObject</em>])</code>
3598
- @param callback thisObject Function to execute for each element.Object to use
3599
- as this when executing callback.
3600
- @name forEach
3601
- @methodOf Array#
3602
- */
3603
- /**
3604
- Tests whether all elements in the array pass the test implemented by the
3605
- provided function.
3606
-
3607
- <code><em>array</em>.every(<em>callback</em>[, <em>thisObject</em>])</code>
3608
- @param callbackthisObject Function to test for each element.Object to use as
3609
- this when executing callback.
3610
- @name every
3611
- @methodOf Array#
3612
- */
3613
- /**
3614
- Creates a new array with the results of calling a provided function on every
3615
- element in this array.
3616
-
3617
- <code><em>array</em>.map(<em>callback</em>[, <em>thisObject</em>])</code>
3618
- @param callbackthisObject Function that produces an element of the new Array
3619
- from an element of the current one.Object to use as this when executing
3620
- callback.
3621
- @name map
3622
- @methodOf Array#
3623
- */
3624
- /**
3625
- Tests whether some element in the array passes the test implemented by the
3626
- provided function.
3627
-
3628
- <code><em>array</em>.some(<em>callback</em>[, <em>thisObject</em>])</code>
3629
- @param callback thisObject Function to test for each element.Object to use as
3630
- this when executing callback.
3631
- @name some
3632
- @methodOf Array#
3633
- */
3634
- /**
3635
- Apply a function against an accumulator and each value of the array (from
3636
- left-to-right) as to reduce it to a single value.
3637
-
3638
- <code><em>array</em>.reduce(<em>callback</em>[, <em>initialValue</em>])</code>
3639
- @param callbackinitialValue Function to execute on each value in the
3640
- array.Object to use as the first argument to the first call of the callback.
3641
- @name reduce
3642
- @methodOf Array#
3643
- */
3644
- /**
3645
- Apply a function simultaneously against two values of the array (from
3646
- right-to-left) as to reduce it to a single value.
3647
-
3648
- <code><em>array</em>.reduceRight(<em>callback</em>[, <em>initialValue</em>])</code>
3649
- @param callback initialValue Function to execute on each value in the
3650
- array.Object to use as the first argument to the first call of the callback.
3651
- @name reduceRight
3652
- @methodOf Array#
3653
- */
3654
- /**
3655
- Returns a boolean indicating whether the object has the specified property.
3656
-
3657
- <code><em>obj</em>.hasOwnProperty(<em>prop</em>)</code>
3658
- @param prop The name of the property to test.
3659
- @name hasOwnProperty
3660
- @methodOf Object#
3661
- */
3662
- /**
3663
- Calls a function with a given this value and arguments provided as an array.
3664
-
3665
- <code><em>fun</em>.apply(<em>thisArg</em>[, <em>argsArray</em>])</code>
3666
- @param thisArg Determines the value of this inside fun. If thisArg is null or
3667
- undefined, this will be the global object. Otherwise, this will be equal to
3668
- Object(thisArg) (which is thisArg if thisArg is already an object, or a String,
3669
- Boolean, or Number if thisArg is a primitive value of the corresponding type).
3670
- Therefore, it is always true that typeof this == "object" when the function
3671
- executes.
3672
- @param argsArray An argument array for the object, specifying the arguments
3673
- with which fun should be called, or null or undefined if no arguments should be
3674
- provided to the function.
3675
- @name apply
3676
- @methodOf Function#
3677
- */
3678
- /**
3679
- Creates a new function that, when called, itself calls this function in the
3680
- context of the provided this value, with a given sequence of arguments preceding
3681
- any provided when the new function was called.
3682
-
3683
- <code><em>fun</em>.bind(<em>thisArg</em>[, <em>arg1</em>[, <em>arg2</em>[, ...]]])</code>
3684
- @param thisValuearg1, arg2, ... The value to be passed as the this parameter to
3685
- the target function when the bound function is called. The value is ignored if
3686
- the bound function is constructed using the new operator.Arguments to prepend to
3687
- arguments provided to the bound function when invoking the target function.
3688
- @name bind
3689
- @methodOf Function#
3690
- */
3691
- /**
3692
- Calls a function with a given this value and arguments provided individually.
3693
-
3694
- <code><em>fun</em>.call(<em>thisArg</em>[, <em>arg1</em>[, <em>arg2</em>[, ...]]])</code>
3695
- @param thisArg Determines the value of this inside fun. If thisArg is null or
3696
- undefined, this will be the global object. Otherwise, this will be equal to
3697
- Object(thisArg) (which is thisArg if thisArg is already an object, or a String,
3698
- Boolean, or Number if thisArg is a primitive value of the corresponding type).
3699
- Therefore, it is always true that typeof this == "object" when the function
3700
- executes.
3701
- @param arg1, arg2, ... Arguments for the object.
3702
- @name call
3703
- @methodOf Function#
3704
- */
3705
- /**
3706
- Returns a string representing the source code of the function.
3707
-
3708
- <code><em>function</em>.toString(<em>indentation</em>)</code>
3709
- @param indentation Non-standard The amount of spaces to indent the string
3710
- representation of the source code. If indentation is less than or equal to -1,
3711
- most unnecessary spaces are removed.
3712
- @name toString
3713
- @methodOf Function#
3714
- */
3715
- /**
3716
- Executes a search for a match in a specified string. Returns a result array, or
3717
- null.
3718
-
3719
-
3720
- @param regexp The name of the regular expression. It can be a variable name or
3721
- a literal.
3722
- @param str The string against which to match the regular expression.
3723
- @name exec
3724
- @methodOf RegExp#
3725
- */
3726
- /**
3727
- Executes the search for a match between a regular expression and a specified
3728
- string. Returns true or false.
3729
-
3730
- <code> <em>regexp</em>.test([<em>str</em>]) </code>
3731
- @param regexp The name of the regular expression. It can be a variable name or
3732
- a literal.
3733
- @param str The string against which to match the regular expression.
3734
- @name test
3735
- @methodOf RegExp#
3736
- */
3737
- /**
3738
- Returns a string representing the specified object.
3739
-
3740
- <code><i>regexp</i>.toString()</code>
3741
-
3742
- @name toString
3743
- @methodOf RegExp#
3744
- */
3745
- /**
3746
- Returns a reference to the Date function that created the instance's prototype.
3747
- Note that the value of this property is a reference to the function itself, not
3748
- a string containing the function's name.
3749
-
3750
-
3751
-
3752
- @name constructor
3753
- @methodOf Date#
3754
- */
3755
- /**
3756
- Returns the day of the month for the specified date according to local time.
3757
-
3758
- <code>
3759
- getDate()
3760
- </code>
3761
-
3762
- @name getDate
3763
- @methodOf Date#
3764
- */
3765
- /**
3766
- Returns the day of the week for the specified date according to local time.
3767
-
3768
- <code>
3769
- getDay()
3770
- </code>
3771
-
3772
- @name getDay
3773
- @methodOf Date#
3774
- */
3775
- /**
3776
- Returns the year of the specified date according to local time.
3777
-
3778
- <code>
3779
- getFullYear()
3780
- </code>
3781
-
3782
- @name getFullYear
3783
- @methodOf Date#
3784
- */
3785
- /**
3786
- Returns the hour for the specified date according to local time.
3787
-
3788
- <code>
3789
- getHours()
3790
- </code>
3791
-
3792
- @name getHours
3793
- @methodOf Date#
3794
- */
3795
- /**
3796
- Returns the milliseconds in the specified date according to local time.
3797
-
3798
- <code>
3799
- getMilliseconds()
3800
- </code>
3801
-
3802
- @name getMilliseconds
3803
- @methodOf Date#
3804
- */
3805
- /**
3806
- Returns the minutes in the specified date according to local time.
3807
-
3808
- <code>
3809
- getMinutes()
3810
- </code>
3811
-
3812
- @name getMinutes
3813
- @methodOf Date#
3814
- */
3815
- /**
3816
- Returns the month in the specified date according to local time.
3817
-
3818
- <code>
3819
- getMonth()
3820
- </code>
3821
-
3822
- @name getMonth
3823
- @methodOf Date#
3824
- */
3825
- /**
3826
- Returns the seconds in the specified date according to local time.
3827
-
3828
- <code>
3829
- getSeconds()
3830
- </code>
3831
-
3832
- @name getSeconds
3833
- @methodOf Date#
3834
- */
3835
- /**
3836
- Returns the numeric value corresponding to the time for the specified date
3837
- according to universal time.
3838
-
3839
- <code> getTime() </code>
3840
-
3841
- @name getTime
3842
- @methodOf Date#
3843
- */
3844
- /**
3845
- Returns the time-zone offset from UTC, in minutes, for the current locale.
3846
-
3847
- <code> getTimezoneOffset() </code>
3848
-
3849
- @name getTimezoneOffset
3850
- @methodOf Date#
3851
- */
3852
- /**
3853
- Returns the day (date) of the month in the specified date according to universal
3854
- time.
3855
-
3856
- <code>
3857
- getUTCDate()
3858
- </code>
3859
-
3860
- @name getUTCDate
3861
- @methodOf Date#
3862
- */
3863
- /**
3864
- Returns the day of the week in the specified date according to universal time.
3865
-
3866
- <code>
3867
- getUTCDay()
3868
- </code>
3869
-
3870
- @name getUTCDay
3871
- @methodOf Date#
3872
- */
3873
- /**
3874
- Returns the year in the specified date according to universal time.
3875
-
3876
- <code>
3877
- getUTCFullYear()
3878
- </code>
3879
-
3880
- @name getUTCFullYear
3881
- @methodOf Date#
3882
- */
3883
- /**
3884
- Returns the hours in the specified date according to universal time.
3885
-
3886
- <code>
3887
- getUTCHours
3888
- </code>
3889
-
3890
- @name getUTCHours
3891
- @methodOf Date#
3892
- */
3893
- /**
3894
- Returns the milliseconds in the specified date according to universal time.
3895
-
3896
- <code>
3897
- getUTCMilliseconds()
3898
- </code>
3899
-
3900
- @name getUTCMilliseconds
3901
- @methodOf Date#
3902
- */
3903
- /**
3904
- Returns the minutes in the specified date according to universal time.
3905
-
3906
- <code>
3907
- getUTCMinutes()
3908
- </code>
3909
-
3910
- @name getUTCMinutes
3911
- @methodOf Date#
3912
- */
3913
- /**
3914
- Returns the month of the specified date according to universal time.
3915
-
3916
- <code>
3917
- getUTCMonth()
3918
- </code>
3919
-
3920
- @name getUTCMonth
3921
- @methodOf Date#
3922
- */
3923
- /**
3924
- Returns the seconds in the specified date according to universal time.
3925
-
3926
- <code>
3927
- getUTCSeconds()
3928
- </code>
3929
-
3930
- @name getUTCSeconds
3931
- @methodOf Date#
3932
- */
3933
- /**
3934
- Sets the day of the month for a specified date according to local time.
3935
-
3936
- <code> setDate(<em>dayValue</em>) </code>
3937
- @param dayValue An integer from 1 to 31, representing the day of the month.
3938
- @name setDate
3939
- @methodOf Date#
3940
- */
3941
- /**
3942
- Sets the full year for a specified date according to local time.
3943
-
3944
- <code>
3945
- setFullYear(<i>yearValue</i>[, <i>monthValue</i>[, <em>dayValue</em>]])
3946
- </code>
3947
- @param yearValue An integer specifying the numeric value of the year, for
3948
- example, 1995.
3949
- @param monthValue An integer between 0 and 11 representing the months January
3950
- through December.
3951
- @param dayValue An integer between 1 and 31 representing the day of the
3952
- month. If you specify the dayValue parameter, you must also specify the
3953
- monthValue.
3954
- @name setFullYear
3955
- @methodOf Date#
3956
- */
3957
- /**
3958
- Sets the hours for a specified date according to local time.
3959
-
3960
- <code>
3961
- setHours(<i>hoursValue</i>[, <i>minutesValue</i>[, <i>secondsValue</i>[, <em>msValue</em>]]])
3962
- </code>
3963
- @param hoursValue An integer between 0 and 23, representing the hour.
3964
- @param minutesValue An integer between 0 and 59, representing the minutes.
3965
- @param secondsValue An integer between 0 and 59, representing the seconds. If
3966
- you specify the secondsValue parameter, you must also specify the minutesValue.
3967
- @param msValue A number between 0 and 999, representing the milliseconds. If
3968
- you specify the msValue parameter, you must also specify the minutesValue and
3969
- secondsValue.
3970
- @name setHours
3971
- @methodOf Date#
3972
- */
3973
- /**
3974
- Sets the milliseconds for a specified date according to local time.
3975
-
3976
- <code>
3977
- setMilliseconds(<i>millisecondsValue</i>)
3978
- </code>
3979
- @param millisecondsValue A number between 0 and 999, representing the
3980
- milliseconds.
3981
- @name setMilliseconds
3982
- @methodOf Date#
3983
- */
3984
- /**
3985
- Sets the minutes for a specified date according to local time.
3986
-
3987
- <code>
3988
- setMinutes(<i>minutesValue</i>[, <i>secondsValue</i>[, <em>msValue</em>]])
3989
- </code>
3990
- @param minutesValue An integer between 0 and 59, representing the minutes.
3991
- @param secondsValue An integer between 0 and 59, representing the seconds. If
3992
- you specify the secondsValue parameter, you must also specify the minutesValue.
3993
- @param msValue A number between 0 and 999, representing the milliseconds. If
3994
- you specify the msValue parameter, you must also specify the minutesValue and
3995
- secondsValue.
3996
- @name setMinutes
3997
- @methodOf Date#
3998
- */
3999
- /**
4000
- Set the month for a specified date according to local time.
4001
-
4002
- <code>
4003
- setMonth(<i>monthValue</i>[, <em>dayValue</em>])
4004
- </code>
4005
- @param monthValue An integer between 0 and 11 (representing the months
4006
- January through December).
4007
- @param dayValue An integer from 1 to 31, representing the day of the month.
4008
- @name setMonth
4009
- @methodOf Date#
4010
- */
4011
- /**
4012
- Sets the seconds for a specified date according to local time.
4013
-
4014
- <code>
4015
- setSeconds(<i>secondsValue</i>[, <em>msValue</em>])
4016
- </code>
4017
- @param secondsValue An integer between 0 and 59.
4018
- @param msValue A number between 0 and 999, representing the milliseconds.
4019
- @name setSeconds
4020
- @methodOf Date#
4021
- */
4022
- /**
4023
- Sets the Date object to the time represented by a number of milliseconds since
4024
- January 1, 1970, 00:00:00 UTC.
4025
-
4026
- <code>
4027
- setTime(<i>timeValue</i>)
4028
- </code>
4029
- @param timeValue An integer representing the number of milliseconds since 1
4030
- January 1970, 00:00:00 UTC.
4031
- @name setTime
4032
- @methodOf Date#
4033
- */
4034
- /**
4035
- Sets the day of the month for a specified date according to universal time.
4036
-
4037
- <code>
4038
- setUTCDate(<i>dayValue</i>)
4039
- </code>
4040
- @param dayValue An integer from 1 to 31, representing the day of the month.
4041
- @name setUTCDate
4042
- @methodOf Date#
4043
- */
4044
- /**
4045
- Sets the full year for a specified date according to universal time.
4046
-
4047
- <code>
4048
- setUTCFullYear(<i>yearValue</i>[, <i>monthValue</i>[, <em>dayValue</em>]])
4049
- </code>
4050
- @param yearValue An integer specifying the numeric value of the year, for
4051
- example, 1995.
4052
- @param monthValue An integer between 0 and 11 representing the months January
4053
- through December.
4054
- @param dayValue An integer between 1 and 31 representing the day of the
4055
- month. If you specify the dayValue parameter, you must also specify the
4056
- monthValue.
4057
- @name setUTCFullYear
4058
- @methodOf Date#
4059
- */
4060
- /**
4061
- Sets the hour for a specified date according to universal time.
4062
-
4063
- <code>
4064
- setUTCHours(<i>hoursValue</i>[, <i>minutesValue</i>[, <i>secondsValue</i>[, <em>msValue</em>]]])
4065
- </code>
4066
- @param hoursValue An integer between 0 and 23, representing the hour.
4067
- @param minutesValue An integer between 0 and 59, representing the minutes.
4068
- @param secondsValue An integer between 0 and 59, representing the seconds. If
4069
- you specify the secondsValue parameter, you must also specify the minutesValue.
4070
- @param msValue A number between 0 and 999, representing the milliseconds. If
4071
- you specify the msValue parameter, you must also specify the minutesValue and
4072
- secondsValue.
4073
- @name setUTCHours
4074
- @methodOf Date#
4075
- */
4076
- /**
4077
- Sets the milliseconds for a specified date according to universal time.
4078
-
4079
- <code>
4080
- setUTCMilliseconds(<i>millisecondsValue</i>)
4081
- </code>
4082
- @param millisecondsValue A number between 0 and 999, representing the
4083
- milliseconds.
4084
- @name setUTCMilliseconds
4085
- @methodOf Date#
4086
- */
4087
- /**
4088
- Sets the minutes for a specified date according to universal time.
4089
-
4090
- <code>
4091
- setUTCMinutes(<i>minutesValue</i>[, <i>secondsValue</i>[, <em>msValue</em>]])
4092
- </code>
4093
- @param minutesValue An integer between 0 and 59, representing the minutes.
4094
- @param secondsValue An integer between 0 and 59, representing the seconds. If
4095
- you specify the secondsValue parameter, you must also specify the minutesValue.
4096
- @param msValue A number between 0 and 999, representing the milliseconds. If
4097
- you specify the msValue parameter, you must also specify the minutesValue and
4098
- secondsValue.
4099
- @name setUTCMinutes
4100
- @methodOf Date#
4101
- */
4102
- /**
4103
- Sets the month for a specified date according to universal time.
4104
-
4105
- <code>
4106
- setUTCMonth(<i>monthValue</i>[, <em>dayValue</em>])
4107
- </code>
4108
- @param monthValue An integer between 0 and 11, representing the months
4109
- January through December.
4110
- @param dayValue An integer from 1 to 31, representing the day of the month.
4111
- @name setUTCMonth
4112
- @methodOf Date#
4113
- */
4114
- /**
4115
- Sets the seconds for a specified date according to universal time.
4116
-
4117
- <code>
4118
- setUTCSeconds(<i>secondsValue</i>[, <em>msValue</em>])
4119
- </code>
4120
- @param secondsValue An integer between 0 and 59.
4121
- @param msValue A number between 0 and 999, representing the milliseconds.
4122
- @name setUTCSeconds
4123
- @methodOf Date#
4124
- */
4125
- /**
4126
- Returns the date portion of a Date object in human readable form in American
4127
- English.
4128
-
4129
- <code><em>date</em>.toDateString()</code>
4130
-
4131
- @name toDateString
4132
- @methodOf Date#
4133
- */
4134
- /**
4135
- Returns a JSON representation of the Date object.
4136
-
4137
- <code><em>date</em>.prototype.toJSON()</code>
4138
-
4139
- @name toJSON
4140
- @methodOf Date#
4141
- */
4142
- /**
4143
- Converts a date to a string, returning the "date" portion using the operating
4144
- system's locale's conventions.
4145
-
4146
- <code>
4147
- toLocaleDateString()
4148
- </code>
4149
-
4150
- @name toLocaleDateString
4151
- @methodOf Date#
4152
- */
4153
- /**
4154
- Converts a date to a string, using the operating system's locale's conventions.
4155
-
4156
- <code>
4157
- toLocaleString()
4158
- </code>
4159
-
4160
- @name toLocaleString
4161
- @methodOf Date#
4162
- */
4163
- /**
4164
- Converts a date to a string, returning the "time" portion using the current
4165
- locale's conventions.
4166
-
4167
- <code> toLocaleTimeString() </code>
4168
-
4169
- @name toLocaleTimeString
4170
- @methodOf Date#
4171
- */
4172
- /**
4173
- Returns a string representing the specified Date object.
4174
-
4175
- <code> toString() </code>
4176
-
4177
- @name toString
4178
- @methodOf Date#
4179
- */
4180
- /**
4181
- Returns the time portion of a Date object in human readable form in American
4182
- English.
4183
-
4184
- <code><em>date</em>.toTimeString()</code>
4185
-
4186
- @name toTimeString
4187
- @methodOf Date#
4188
- */
4189
- /**
4190
- Converts a date to a string, using the universal time convention.
4191
-
4192
- <code> toUTCString() </code>
4193
-
4194
- @name toUTCString
4195
- @methodOf Date#
4196
- */
4197
-
4198
-
4199
- /*!
4200
- Math.uuid.js (v1.4)
4201
- http://www.broofa.com
4202
- mailto:robert@broofa.com
4203
-
4204
- Copyright (c) 2010 Robert Kieffer
4205
- Dual licensed under the MIT and GPL licenses.
4206
- */
4207
-
4208
- /**
4209
- Generate a random uuid.
4210
-
4211
- <code><pre>
4212
- // No arguments - returns RFC4122, version 4 ID
4213
- Math.uuid()
4214
- => "92329D39-6F5C-4520-ABFC-AAB64544E172"
4215
-
4216
- // One argument - returns ID of the specified length
4217
- Math.uuid(15) // 15 character ID (default base=62)
4218
- => "VcydxgltxrVZSTV"
4219
-
4220
- // Two arguments - returns ID of the specified length, and radix. (Radix must be <= 62)
4221
- Math.uuid(8, 2) // 8 character ID (base=2)
4222
- => "01001010"
4223
-
4224
- Math.uuid(8, 10) // 8 character ID (base=10)
4225
- => "47473046"
4226
-
4227
- Math.uuid(8, 16) // 8 character ID (base=16)
4228
- => "098F4D35"
4229
- </pre></code>
4230
-
4231
- @name uuid
4232
- @methodOf Math
4233
- @param length The desired number of characters
4234
- @param radix The number of allowable values for each character.
4235
- */
4236
- (function() {
4237
- // Private array of chars to use
4238
- var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
4239
-
4240
- Math.uuid = function (len, radix) {
4241
- var chars = CHARS, uuid = [];
4242
- radix = radix || chars.length;
4243
-
4244
- if (len) {
4245
- // Compact form
4246
- for (var i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
4247
- } else {
4248
- // rfc4122, version 4 form
4249
- var r;
4250
-
4251
- // rfc4122 requires these characters
4252
- uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
4253
- uuid[14] = '4';
4254
-
4255
- // Fill in random data. At i==19 set the high bits of clock sequence as
4256
- // per rfc4122, sec. 4.1.5
4257
- for (var i = 0; i < 36; i++) {
4258
- if (!uuid[i]) {
4259
- r = 0 | Math.random()*16;
4260
- uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
4261
- }
4262
- }
4263
- }
4264
-
4265
- return uuid.join('');
4266
- };
4267
-
4268
- // A more performant, but slightly bulkier, RFC4122v4 solution. We boost performance
4269
- // by minimizing calls to random()
4270
- Math.uuidFast = function() {
4271
- var chars = CHARS, uuid = new Array(36), rnd=0, r;
4272
- for (var i = 0; i < 36; i++) {
4273
- if (i==8 || i==13 || i==18 || i==23) {
4274
- uuid[i] = '-';
4275
- } else if (i==14) {
4276
- uuid[i] = '4';
4277
- } else {
4278
- if (rnd <= 0x02) rnd = 0x2000000 + (Math.random()*0x1000000)|0;
4279
- r = rnd & 0xf;
4280
- rnd = rnd >> 4;
4281
- uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
4282
- }
4283
- }
4284
- return uuid.join('');
4285
- };
4286
-
4287
- // A more compact, but less performant, RFC4122v4 solution:
4288
- Math.uuidCompact = function() {
4289
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
4290
- var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
4291
- return v.toString(16);
4292
- }).toUpperCase();
4293
- };
4294
- })();
4295
-