front_end_tasks 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +187 -0
- data/Rakefile +1 -0
- data/bin/fe +4 -0
- data/front_end_tasks.gemspec +31 -0
- data/lib/front_end_tasks.rb +42 -0
- data/lib/front_end_tasks/cli.rb +45 -0
- data/lib/front_end_tasks/documents.rb +8 -0
- data/lib/front_end_tasks/documents/asset_document.rb +15 -0
- data/lib/front_end_tasks/documents/base_document.rb +18 -0
- data/lib/front_end_tasks/documents/css_document.rb +41 -0
- data/lib/front_end_tasks/documents/html_document.rb +143 -0
- data/lib/front_end_tasks/documents/js_document.rb +79 -0
- data/lib/front_end_tasks/lint.rb +17 -0
- data/lib/front_end_tasks/optimizer.rb +39 -0
- data/lib/front_end_tasks/server.rb +14 -0
- data/lib/front_end_tasks/spec.rb +58 -0
- data/lib/front_end_tasks/version.rb +3 -0
- data/vendor/es5-shim/es5-shim.js +1390 -0
- data/vendor/es5-shim/es5-shim.min.js +7 -0
- data/vendor/worker-shim/worker-shim.js +1 -0
- metadata +196 -0
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'front_end_tasks/documents/base_document'
|
3
|
+
require 'front_end_tasks/documents/js_document'
|
4
|
+
require 'front_end_tasks/documents/css_document'
|
5
|
+
|
6
|
+
module FrontEndTasks
|
7
|
+
module Documents
|
8
|
+
class HtmlDocument < BaseDocument
|
9
|
+
|
10
|
+
def initialize(public_root, content)
|
11
|
+
super(public_root, content)
|
12
|
+
@doc = Nokogiri::HTML(content)
|
13
|
+
end
|
14
|
+
|
15
|
+
def compile
|
16
|
+
path_content_pairs = {}
|
17
|
+
|
18
|
+
script_groups.each do |group|
|
19
|
+
combined_content = group[:files].inject('') { |content, file| content << File.read(file) }
|
20
|
+
combined_file_path = group[:combined_file_path]
|
21
|
+
js_document = JsDocument.new(@public_root, combined_content)
|
22
|
+
js_document.compiled_path = combined_file_path
|
23
|
+
new_files = js_document.compile
|
24
|
+
|
25
|
+
script_node = Nokogiri::XML::Node.new("script", @doc)
|
26
|
+
script_node[:src] = combined_file_path
|
27
|
+
replace_group(group, script_node)
|
28
|
+
|
29
|
+
path_content_pairs.merge!(new_files)
|
30
|
+
end
|
31
|
+
|
32
|
+
style_groups.each do |group|
|
33
|
+
combined_file_path = group[:combined_file_path]
|
34
|
+
combined_content = ''
|
35
|
+
|
36
|
+
group[:files].each do |file|
|
37
|
+
content = File.read(file)
|
38
|
+
updated_content, assets = CssDocument.find_and_update_url_references(File.dirname(file), File.dirname(combined_file_path), content)
|
39
|
+
|
40
|
+
assets.each do |asset|
|
41
|
+
new_files = asset.compile
|
42
|
+
path_content_pairs.merge!(new_files)
|
43
|
+
end
|
44
|
+
|
45
|
+
combined_content << updated_content
|
46
|
+
end
|
47
|
+
|
48
|
+
css_document = CssDocument.new(@public_root, combined_content)
|
49
|
+
css_document.compiled_path = combined_file_path
|
50
|
+
new_files = css_document.compile
|
51
|
+
|
52
|
+
link_node = Nokogiri::XML::Node.new("link", @doc)
|
53
|
+
link_node[:href] = combined_file_path
|
54
|
+
link_node[:rel] = "stylesheet"
|
55
|
+
replace_group(group, link_node)
|
56
|
+
|
57
|
+
path_content_pairs.merge!(new_files)
|
58
|
+
end
|
59
|
+
|
60
|
+
comments.each { |c| c.remove }
|
61
|
+
|
62
|
+
path_content_pairs.merge!({
|
63
|
+
@compiled_path => @doc.to_html.gsub(/\n\s*\n/, "\n")
|
64
|
+
})
|
65
|
+
|
66
|
+
path_content_pairs
|
67
|
+
end
|
68
|
+
|
69
|
+
def included_scripts(public_root = nil)
|
70
|
+
script_nodes = @doc.xpath('//script')
|
71
|
+
script_nodes.map { |n| n[:src] }
|
72
|
+
script_nodes.map do |node|
|
73
|
+
if public_root
|
74
|
+
File.expand_path(File.join(public_root, node[:src]))
|
75
|
+
else
|
76
|
+
node[:src]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
protected
|
82
|
+
|
83
|
+
def script_groups
|
84
|
+
groups = groups_matching_opening_comment(/\s?build:script (\S+)\s?$/, :tag_name => 'script')
|
85
|
+
groups.each do |group|
|
86
|
+
group[:files] = group[:elements].map { |e| File.join(@public_root, e[:src]) }
|
87
|
+
group[:combined_file_path] = group[:args][0]
|
88
|
+
end
|
89
|
+
groups
|
90
|
+
end
|
91
|
+
|
92
|
+
def style_groups
|
93
|
+
groups = groups_matching_opening_comment(/\s?build:style (\S+)\s?$/, :tag_name => 'link')
|
94
|
+
groups.each do |group|
|
95
|
+
group[:files] = group[:elements].map { |e| File.join(@public_root, e[:href]) }
|
96
|
+
group[:combined_file_path] = group[:args][0]
|
97
|
+
end
|
98
|
+
groups
|
99
|
+
end
|
100
|
+
|
101
|
+
def groups_matching_opening_comment(comment_regex, opts)
|
102
|
+
tag_name = opts[:tag_name]
|
103
|
+
groups = []
|
104
|
+
comments.each do |comment|
|
105
|
+
if comment.content =~ comment_regex
|
106
|
+
opening_comment = comment
|
107
|
+
elements = []
|
108
|
+
|
109
|
+
# collect capture groups from comment_regex
|
110
|
+
matches = $~.to_a
|
111
|
+
matches.shift
|
112
|
+
args = matches
|
113
|
+
|
114
|
+
# collect all elements with tag_name that follow the opening_comment
|
115
|
+
next_element = opening_comment.next_element
|
116
|
+
while (next_element && next_element.name == tag_name) do
|
117
|
+
elements << next_element
|
118
|
+
next_element = next_element.next_element
|
119
|
+
end
|
120
|
+
|
121
|
+
groups << {
|
122
|
+
:opening_comment => opening_comment,
|
123
|
+
:elements => elements,
|
124
|
+
:args => args
|
125
|
+
}
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
groups
|
130
|
+
end
|
131
|
+
|
132
|
+
def comments
|
133
|
+
@comments ||= @doc.xpath('//comment()')
|
134
|
+
end
|
135
|
+
|
136
|
+
def replace_group(group, new_element)
|
137
|
+
group[:elements].each { |e| e.remove }
|
138
|
+
group[:opening_comment].add_next_sibling(new_element)
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'uglifier'
|
2
|
+
require 'front_end_tasks/documents/base_document'
|
3
|
+
|
4
|
+
module FrontEndTasks
|
5
|
+
module Documents
|
6
|
+
class JsDocument < BaseDocument
|
7
|
+
|
8
|
+
def compile
|
9
|
+
path_content_pairs = {}
|
10
|
+
|
11
|
+
workers = find_worker_references(@public_root, @content)
|
12
|
+
workers.each do |worker|
|
13
|
+
new_files = worker.compile
|
14
|
+
path_content_pairs.merge!(new_files)
|
15
|
+
end
|
16
|
+
|
17
|
+
@content = replace_worker_import_scripts(@public_root, @content)
|
18
|
+
|
19
|
+
compiled_content = Uglifier.compile(@content)
|
20
|
+
path_content_pairs.merge!({
|
21
|
+
@compiled_path => compiled_content
|
22
|
+
})
|
23
|
+
|
24
|
+
path_content_pairs
|
25
|
+
end
|
26
|
+
|
27
|
+
def included_scripts(public_root = nil)
|
28
|
+
paths = []
|
29
|
+
|
30
|
+
import_scripts = @content.scan(/importScripts\(([^)]+)\)/)
|
31
|
+
import_scripts.each do |import_script|
|
32
|
+
argument_content = import_script[0]
|
33
|
+
paths = argument_content.split(",").map { |p| p.strip.chop.reverse.chop.reverse }
|
34
|
+
end
|
35
|
+
|
36
|
+
paths.map do |path|
|
37
|
+
if public_root
|
38
|
+
File.expand_path(File.join(public_root, path))
|
39
|
+
else
|
40
|
+
path
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
protected
|
46
|
+
def find_worker_references(public_root, content)
|
47
|
+
workers = []
|
48
|
+
|
49
|
+
worker_references = content.scan(/Worker\(['"](.*)['"]\)/)
|
50
|
+
worker_references.each do |worker_reference|
|
51
|
+
url = worker_reference[0].strip
|
52
|
+
filename = File.basename(url).split("?")[0].split("#")[0]
|
53
|
+
local_file_path = File.expand_path(File.join(public_root, File.dirname(url), filename))
|
54
|
+
|
55
|
+
# get worker contents
|
56
|
+
worker = self.class.new(public_root, File.read(local_file_path))
|
57
|
+
worker.compiled_path = url
|
58
|
+
workers << worker
|
59
|
+
end
|
60
|
+
|
61
|
+
workers
|
62
|
+
end
|
63
|
+
|
64
|
+
def replace_worker_import_scripts(public_root, content)
|
65
|
+
updated_content = ''
|
66
|
+
|
67
|
+
included_scripts(public_root).each do |path|
|
68
|
+
updated_content << File.read(path)
|
69
|
+
end
|
70
|
+
|
71
|
+
# append the rest of the worker contents
|
72
|
+
updated_content << content.gsub(/importScripts\(([^)]+)\);/, '')
|
73
|
+
|
74
|
+
updated_content
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'jshintrb'
|
2
|
+
|
3
|
+
module FrontEndTasks
|
4
|
+
class Lint
|
5
|
+
|
6
|
+
def self.report(files)
|
7
|
+
report = Jshintrb.report(files)
|
8
|
+
if report.length > 0
|
9
|
+
puts report
|
10
|
+
else
|
11
|
+
count = files.count
|
12
|
+
puts "#{count} #{count == 1 ? 'file' : 'files' } lint free."
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'front_end_tasks/documents'
|
3
|
+
|
4
|
+
module FrontEndTasks
|
5
|
+
|
6
|
+
class Optimizer
|
7
|
+
|
8
|
+
def initialize(public_dir, files)
|
9
|
+
@public_dir = File.expand_path(public_dir)
|
10
|
+
@files = files.map { |f| File.expand_path(f) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def build_to(result_dir)
|
14
|
+
@files.each do |file|
|
15
|
+
html_filename = File.basename(file)
|
16
|
+
html_doc = Documents::HtmlDocument.new(@public_dir, File.read(file))
|
17
|
+
html_doc.compiled_path = html_filename
|
18
|
+
|
19
|
+
files = html_doc.compile
|
20
|
+
|
21
|
+
files.each_pair do |file, contents|
|
22
|
+
save_file(File.join(result_dir, file), contents)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
|
29
|
+
def save_file(file_with_path, contents)
|
30
|
+
full_path = File.expand_path(file_with_path)
|
31
|
+
directory = File.dirname(full_path)
|
32
|
+
|
33
|
+
FileUtils.mkdir_p directory
|
34
|
+
File.write(full_path, contents)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'webrick'
|
2
|
+
|
3
|
+
module FrontEndTasks
|
4
|
+
class Server
|
5
|
+
include WEBrick
|
6
|
+
|
7
|
+
def self.start(opts)
|
8
|
+
server = HTTPServer.new(:Port => opts[:port], :DocumentRoot => opts[:public_dir])
|
9
|
+
trap("INT") { server.shutdown }
|
10
|
+
server.start
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'jasmine'
|
2
|
+
|
3
|
+
module FrontEndTasks
|
4
|
+
class Spec
|
5
|
+
ES5_SHIM = File.expand_path(File.join(__dir__, '../..', 'vendor/es5-shim/es5-shim.js'))
|
6
|
+
WORKER_SHIM = File.expand_path(File.join(__dir__, '../..', 'vendor/worker-shim/worker-shim.js'))
|
7
|
+
|
8
|
+
# mostly taken from https://github.com/pivotal/jasmine-gem/blob/master/lib/jasmine/tasks/jasmine.rake
|
9
|
+
def self.run(opts)
|
10
|
+
config = Jasmine.config
|
11
|
+
config.src_dir = File.expand_path('./')
|
12
|
+
config.spec_dir = File.expand_path('./')
|
13
|
+
config.ci_port = opts[:port]
|
14
|
+
|
15
|
+
if opts[:source_files]
|
16
|
+
source_files = opts[:source_files]
|
17
|
+
elsif opts[:worker_file]
|
18
|
+
source_files = []
|
19
|
+
source_files << '__worker-shim.js__'
|
20
|
+
config.add_rack_path('/__worker-shim.js__', lambda { Rack::File.new(WORKER_SHIM) })
|
21
|
+
js_doc = Documents::JsDocument.new(nil, File.read(opts[:worker_file]))
|
22
|
+
source_files += js_doc.included_scripts.map { |s| File.join(opts[:public_root], s) }
|
23
|
+
source_files << opts[:worker_file]
|
24
|
+
end
|
25
|
+
|
26
|
+
helper_files = opts[:helper_files] || []
|
27
|
+
|
28
|
+
# hack the es5-shim to load before src files
|
29
|
+
config.add_rack_path('/__es5-shim.js__', lambda { Rack::File.new(ES5_SHIM) })
|
30
|
+
config.src_files = lambda { ['__es5-shim.js__'] + source_files }
|
31
|
+
config.spec_files = lambda { helper_files + opts[:spec_files] }
|
32
|
+
|
33
|
+
server = Jasmine::Server.new(config.port(:ci), Jasmine::Application.app(config))
|
34
|
+
t = Thread.new do
|
35
|
+
begin
|
36
|
+
server.start
|
37
|
+
rescue ChildProcess::TimeoutError
|
38
|
+
end
|
39
|
+
# # ignore bad exits
|
40
|
+
end
|
41
|
+
t.abort_on_exception = true
|
42
|
+
Jasmine::wait_for_listener(config.port(:ci), 'jasmine server')
|
43
|
+
puts 'jasmine server started.'
|
44
|
+
|
45
|
+
formatters = config.formatters.map { |formatter_class| formatter_class.new }
|
46
|
+
|
47
|
+
exit_code_formatter = Jasmine::Formatters::ExitCode.new
|
48
|
+
formatters << exit_code_formatter
|
49
|
+
|
50
|
+
url = "#{config.host}:#{config.port(:ci)}/"
|
51
|
+
runner = config.runner.call(Jasmine::Formatters::Multi.new(formatters), url)
|
52
|
+
runner.run
|
53
|
+
|
54
|
+
exit(1) unless exit_code_formatter.succeeded?
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,1390 @@
|
|
1
|
+
/*!
|
2
|
+
* https://github.com/es-shims/es5-shim
|
3
|
+
* @license es5-shim Copyright 2009-2014 by contributors, MIT License
|
4
|
+
* see https://github.com/es-shims/es5-shim/blob/master/LICENSE
|
5
|
+
*/
|
6
|
+
|
7
|
+
// vim: ts=4 sts=4 sw=4 expandtab
|
8
|
+
|
9
|
+
//Add semicolon to prevent IIFE from being passed as argument to concated code.
|
10
|
+
;
|
11
|
+
|
12
|
+
// UMD (Universal Module Definition)
|
13
|
+
// see https://github.com/umdjs/umd/blob/master/returnExports.js
|
14
|
+
(function (root, factory) {
|
15
|
+
if (typeof define === 'function' && define.amd) {
|
16
|
+
// AMD. Register as an anonymous module.
|
17
|
+
define(factory);
|
18
|
+
} else if (typeof exports === 'object') {
|
19
|
+
// Node. Does not work with strict CommonJS, but
|
20
|
+
// only CommonJS-like enviroments that support module.exports,
|
21
|
+
// like Node.
|
22
|
+
module.exports = factory();
|
23
|
+
} else {
|
24
|
+
// Browser globals (root is window)
|
25
|
+
root.returnExports = factory();
|
26
|
+
}
|
27
|
+
}(this, function () {
|
28
|
+
|
29
|
+
/**
|
30
|
+
* Brings an environment as close to ECMAScript 5 compliance
|
31
|
+
* as is possible with the facilities of erstwhile engines.
|
32
|
+
*
|
33
|
+
* Annotated ES5: http://es5.github.com/ (specific links below)
|
34
|
+
* ES5 Spec: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
|
35
|
+
* Required reading: http://javascriptweblog.wordpress.com/2011/12/05/extending-javascript-natives/
|
36
|
+
*/
|
37
|
+
|
38
|
+
//
|
39
|
+
// Function
|
40
|
+
// ========
|
41
|
+
//
|
42
|
+
|
43
|
+
// ES-5 15.3.4.5
|
44
|
+
// http://es5.github.com/#x15.3.4.5
|
45
|
+
|
46
|
+
function Empty() {}
|
47
|
+
|
48
|
+
if (!Function.prototype.bind) {
|
49
|
+
Function.prototype.bind = function bind(that) { // .length is 1
|
50
|
+
// 1. Let Target be the this value.
|
51
|
+
var target = this;
|
52
|
+
// 2. If IsCallable(Target) is false, throw a TypeError exception.
|
53
|
+
if (typeof target != "function") {
|
54
|
+
throw new TypeError("Function.prototype.bind called on incompatible " + target);
|
55
|
+
}
|
56
|
+
// 3. Let A be a new (possibly empty) internal list of all of the
|
57
|
+
// argument values provided after thisArg (arg1, arg2 etc), in order.
|
58
|
+
// XXX slicedArgs will stand in for "A" if used
|
59
|
+
var args = _Array_slice_.call(arguments, 1); // for normal call
|
60
|
+
// 4. Let F be a new native ECMAScript object.
|
61
|
+
// 11. Set the [[Prototype]] internal property of F to the standard
|
62
|
+
// built-in Function prototype object as specified in 15.3.3.1.
|
63
|
+
// 12. Set the [[Call]] internal property of F as described in
|
64
|
+
// 15.3.4.5.1.
|
65
|
+
// 13. Set the [[Construct]] internal property of F as described in
|
66
|
+
// 15.3.4.5.2.
|
67
|
+
// 14. Set the [[HasInstance]] internal property of F as described in
|
68
|
+
// 15.3.4.5.3.
|
69
|
+
var binder = function () {
|
70
|
+
|
71
|
+
if (this instanceof bound) {
|
72
|
+
// 15.3.4.5.2 [[Construct]]
|
73
|
+
// When the [[Construct]] internal method of a function object,
|
74
|
+
// F that was created using the bind function is called with a
|
75
|
+
// list of arguments ExtraArgs, the following steps are taken:
|
76
|
+
// 1. Let target be the value of F's [[TargetFunction]]
|
77
|
+
// internal property.
|
78
|
+
// 2. If target has no [[Construct]] internal method, a
|
79
|
+
// TypeError exception is thrown.
|
80
|
+
// 3. Let boundArgs be the value of F's [[BoundArgs]] internal
|
81
|
+
// property.
|
82
|
+
// 4. Let args be a new list containing the same values as the
|
83
|
+
// list boundArgs in the same order followed by the same
|
84
|
+
// values as the list ExtraArgs in the same order.
|
85
|
+
// 5. Return the result of calling the [[Construct]] internal
|
86
|
+
// method of target providing args as the arguments.
|
87
|
+
|
88
|
+
var result = target.apply(
|
89
|
+
this,
|
90
|
+
args.concat(_Array_slice_.call(arguments))
|
91
|
+
);
|
92
|
+
if (Object(result) === result) {
|
93
|
+
return result;
|
94
|
+
}
|
95
|
+
return this;
|
96
|
+
|
97
|
+
} else {
|
98
|
+
// 15.3.4.5.1 [[Call]]
|
99
|
+
// When the [[Call]] internal method of a function object, F,
|
100
|
+
// which was created using the bind function is called with a
|
101
|
+
// this value and a list of arguments ExtraArgs, the following
|
102
|
+
// steps are taken:
|
103
|
+
// 1. Let boundArgs be the value of F's [[BoundArgs]] internal
|
104
|
+
// property.
|
105
|
+
// 2. Let boundThis be the value of F's [[BoundThis]] internal
|
106
|
+
// property.
|
107
|
+
// 3. Let target be the value of F's [[TargetFunction]] internal
|
108
|
+
// property.
|
109
|
+
// 4. Let args be a new list containing the same values as the
|
110
|
+
// list boundArgs in the same order followed by the same
|
111
|
+
// values as the list ExtraArgs in the same order.
|
112
|
+
// 5. Return the result of calling the [[Call]] internal method
|
113
|
+
// of target providing boundThis as the this value and
|
114
|
+
// providing args as the arguments.
|
115
|
+
|
116
|
+
// equiv: target.call(this, ...boundArgs, ...args)
|
117
|
+
return target.apply(
|
118
|
+
that,
|
119
|
+
args.concat(_Array_slice_.call(arguments))
|
120
|
+
);
|
121
|
+
|
122
|
+
}
|
123
|
+
|
124
|
+
};
|
125
|
+
|
126
|
+
// 15. If the [[Class]] internal property of Target is "Function", then
|
127
|
+
// a. Let L be the length property of Target minus the length of A.
|
128
|
+
// b. Set the length own property of F to either 0 or L, whichever is
|
129
|
+
// larger.
|
130
|
+
// 16. Else set the length own property of F to 0.
|
131
|
+
|
132
|
+
var boundLength = Math.max(0, target.length - args.length);
|
133
|
+
|
134
|
+
// 17. Set the attributes of the length own property of F to the values
|
135
|
+
// specified in 15.3.5.1.
|
136
|
+
var boundArgs = [];
|
137
|
+
for (var i = 0; i < boundLength; i++) {
|
138
|
+
boundArgs.push("$" + i);
|
139
|
+
}
|
140
|
+
|
141
|
+
// XXX Build a dynamic function with desired amount of arguments is the only
|
142
|
+
// way to set the length property of a function.
|
143
|
+
// In environments where Content Security Policies enabled (Chrome extensions,
|
144
|
+
// for ex.) all use of eval or Function costructor throws an exception.
|
145
|
+
// However in all of these environments Function.prototype.bind exists
|
146
|
+
// and so this code will never be executed.
|
147
|
+
var bound = Function("binder", "return function(" + boundArgs.join(",") + "){return binder.apply(this,arguments)}")(binder);
|
148
|
+
|
149
|
+
if (target.prototype) {
|
150
|
+
Empty.prototype = target.prototype;
|
151
|
+
bound.prototype = new Empty();
|
152
|
+
// Clean up dangling references.
|
153
|
+
Empty.prototype = null;
|
154
|
+
}
|
155
|
+
|
156
|
+
// TODO
|
157
|
+
// 18. Set the [[Extensible]] internal property of F to true.
|
158
|
+
|
159
|
+
// TODO
|
160
|
+
// 19. Let thrower be the [[ThrowTypeError]] function Object (13.2.3).
|
161
|
+
// 20. Call the [[DefineOwnProperty]] internal method of F with
|
162
|
+
// arguments "caller", PropertyDescriptor {[[Get]]: thrower, [[Set]]:
|
163
|
+
// thrower, [[Enumerable]]: false, [[Configurable]]: false}, and
|
164
|
+
// false.
|
165
|
+
// 21. Call the [[DefineOwnProperty]] internal method of F with
|
166
|
+
// arguments "arguments", PropertyDescriptor {[[Get]]: thrower,
|
167
|
+
// [[Set]]: thrower, [[Enumerable]]: false, [[Configurable]]: false},
|
168
|
+
// and false.
|
169
|
+
|
170
|
+
// TODO
|
171
|
+
// NOTE Function objects created using Function.prototype.bind do not
|
172
|
+
// have a prototype property or the [[Code]], [[FormalParameters]], and
|
173
|
+
// [[Scope]] internal properties.
|
174
|
+
// XXX can't delete prototype in pure-js.
|
175
|
+
|
176
|
+
// 22. Return F.
|
177
|
+
return bound;
|
178
|
+
};
|
179
|
+
}
|
180
|
+
|
181
|
+
// Shortcut to an often accessed properties, in order to avoid multiple
|
182
|
+
// dereference that costs universally.
|
183
|
+
// _Please note: Shortcuts are defined after `Function.prototype.bind` as we
|
184
|
+
// us it in defining shortcuts.
|
185
|
+
var call = Function.prototype.call;
|
186
|
+
var prototypeOfArray = Array.prototype;
|
187
|
+
var prototypeOfObject = Object.prototype;
|
188
|
+
var _Array_slice_ = prototypeOfArray.slice;
|
189
|
+
// Having a toString local variable name breaks in Opera so use _toString.
|
190
|
+
var _toString = call.bind(prototypeOfObject.toString);
|
191
|
+
var owns = call.bind(prototypeOfObject.hasOwnProperty);
|
192
|
+
|
193
|
+
// If JS engine supports accessors creating shortcuts.
|
194
|
+
var defineGetter;
|
195
|
+
var defineSetter;
|
196
|
+
var lookupGetter;
|
197
|
+
var lookupSetter;
|
198
|
+
var supportsAccessors;
|
199
|
+
if ((supportsAccessors = owns(prototypeOfObject, "__defineGetter__"))) {
|
200
|
+
defineGetter = call.bind(prototypeOfObject.__defineGetter__);
|
201
|
+
defineSetter = call.bind(prototypeOfObject.__defineSetter__);
|
202
|
+
lookupGetter = call.bind(prototypeOfObject.__lookupGetter__);
|
203
|
+
lookupSetter = call.bind(prototypeOfObject.__lookupSetter__);
|
204
|
+
}
|
205
|
+
|
206
|
+
//
|
207
|
+
// Array
|
208
|
+
// =====
|
209
|
+
//
|
210
|
+
|
211
|
+
// ES5 15.4.4.12
|
212
|
+
// http://es5.github.com/#x15.4.4.12
|
213
|
+
// Default value for second param
|
214
|
+
// [bugfix, ielt9, old browsers]
|
215
|
+
// IE < 9 bug: [1,2].splice(0).join("") == "" but should be "12"
|
216
|
+
if ([1,2].splice(0).length != 2) {
|
217
|
+
var array_splice = Array.prototype.splice;
|
218
|
+
var array_push = Array.prototype.push;
|
219
|
+
var array_unshift = Array.prototype.unshift;
|
220
|
+
|
221
|
+
if (function() { // test IE < 9 to splice bug - see issue #138
|
222
|
+
function makeArray(l) {
|
223
|
+
var a = [];
|
224
|
+
while (l--) {
|
225
|
+
a.unshift(l)
|
226
|
+
}
|
227
|
+
return a
|
228
|
+
}
|
229
|
+
|
230
|
+
var array = []
|
231
|
+
, lengthBefore
|
232
|
+
;
|
233
|
+
|
234
|
+
array.splice.bind(array, 0, 0).apply(null, makeArray(20));
|
235
|
+
array.splice.bind(array, 0, 0).apply(null, makeArray(26));
|
236
|
+
|
237
|
+
lengthBefore = array.length; //20
|
238
|
+
array.splice(5, 0, "XXX"); // add one element
|
239
|
+
|
240
|
+
if (lengthBefore + 1 == array.length) {
|
241
|
+
return true;// has right splice implementation without bugs
|
242
|
+
}
|
243
|
+
// else {
|
244
|
+
// IE8 bug
|
245
|
+
// }
|
246
|
+
}()) {//IE 6/7
|
247
|
+
Array.prototype.splice = function(start, deleteCount) {
|
248
|
+
if (!arguments.length) {
|
249
|
+
return [];
|
250
|
+
} else {
|
251
|
+
return array_splice.apply(this, [
|
252
|
+
start === void 0 ? 0 : start,
|
253
|
+
deleteCount === void 0 ? (this.length - start) : deleteCount
|
254
|
+
].concat(_Array_slice_.call(arguments, 2)))
|
255
|
+
}
|
256
|
+
};
|
257
|
+
}
|
258
|
+
else {//IE8
|
259
|
+
Array.prototype.splice = function(start, deleteCount) {
|
260
|
+
var result
|
261
|
+
, args = _Array_slice_.call(arguments, 2)
|
262
|
+
, addElementsCount = args.length
|
263
|
+
;
|
264
|
+
|
265
|
+
if (!arguments.length) {
|
266
|
+
return [];
|
267
|
+
}
|
268
|
+
|
269
|
+
if (start === void 0) { // default
|
270
|
+
start = 0;
|
271
|
+
}
|
272
|
+
if (deleteCount === void 0) { // default
|
273
|
+
deleteCount = this.length - start;
|
274
|
+
}
|
275
|
+
|
276
|
+
if (addElementsCount > 0) {
|
277
|
+
if (deleteCount <= 0) {
|
278
|
+
if (start == this.length) { // tiny optimisation #1
|
279
|
+
array_push.apply(this, args);
|
280
|
+
return [];
|
281
|
+
}
|
282
|
+
|
283
|
+
if (start == 0) { // tiny optimisation #2
|
284
|
+
array_unshift.apply(this, args);
|
285
|
+
return [];
|
286
|
+
}
|
287
|
+
}
|
288
|
+
|
289
|
+
// Array.prototype.splice implementation
|
290
|
+
result = _Array_slice_.call(this, start, start + deleteCount);// delete part
|
291
|
+
args.push.apply(args, _Array_slice_.call(this, start + deleteCount, this.length));// right part
|
292
|
+
args.unshift.apply(args, _Array_slice_.call(this, 0, start));// left part
|
293
|
+
|
294
|
+
// delete all items from this array and replace it to 'left part' + _Array_slice_.call(arguments, 2) + 'right part'
|
295
|
+
args.unshift(0, this.length);
|
296
|
+
|
297
|
+
array_splice.apply(this, args);
|
298
|
+
|
299
|
+
return result;
|
300
|
+
}
|
301
|
+
|
302
|
+
return array_splice.call(this, start, deleteCount);
|
303
|
+
}
|
304
|
+
|
305
|
+
}
|
306
|
+
}
|
307
|
+
|
308
|
+
// ES5 15.4.4.12
|
309
|
+
// http://es5.github.com/#x15.4.4.13
|
310
|
+
// Return len+argCount.
|
311
|
+
// [bugfix, ielt8]
|
312
|
+
// IE < 8 bug: [].unshift(0) == undefined but should be "1"
|
313
|
+
if ([].unshift(0) != 1) {
|
314
|
+
var array_unshift = Array.prototype.unshift;
|
315
|
+
Array.prototype.unshift = function() {
|
316
|
+
array_unshift.apply(this, arguments);
|
317
|
+
return this.length;
|
318
|
+
};
|
319
|
+
}
|
320
|
+
|
321
|
+
// ES5 15.4.3.2
|
322
|
+
// http://es5.github.com/#x15.4.3.2
|
323
|
+
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray
|
324
|
+
if (!Array.isArray) {
|
325
|
+
Array.isArray = function isArray(obj) {
|
326
|
+
return _toString(obj) == "[object Array]";
|
327
|
+
};
|
328
|
+
}
|
329
|
+
|
330
|
+
// The IsCallable() check in the Array functions
|
331
|
+
// has been replaced with a strict check on the
|
332
|
+
// internal class of the object to trap cases where
|
333
|
+
// the provided function was actually a regular
|
334
|
+
// expression literal, which in V8 and
|
335
|
+
// JavaScriptCore is a typeof "function". Only in
|
336
|
+
// V8 are regular expression literals permitted as
|
337
|
+
// reduce parameters, so it is desirable in the
|
338
|
+
// general case for the shim to match the more
|
339
|
+
// strict and common behavior of rejecting regular
|
340
|
+
// expressions.
|
341
|
+
|
342
|
+
// ES5 15.4.4.18
|
343
|
+
// http://es5.github.com/#x15.4.4.18
|
344
|
+
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/forEach
|
345
|
+
|
346
|
+
|
347
|
+
// Check failure of by-index access of string characters (IE < 9)
|
348
|
+
// and failure of `0 in boxedString` (Rhino)
|
349
|
+
var boxedString = Object("a");
|
350
|
+
var splitString = boxedString[0] != "a" || !(0 in boxedString);
|
351
|
+
|
352
|
+
var properlyBoxesContext = function properlyBoxed(method) {
|
353
|
+
// Check node 0.6.21 bug where third parameter is not boxed
|
354
|
+
var properlyBoxes = true;
|
355
|
+
if (method) {
|
356
|
+
method.call('foo', function (item, index, context) {
|
357
|
+
if (typeof context !== 'object') { properlyBoxes = false; }
|
358
|
+
});
|
359
|
+
}
|
360
|
+
return !!method && properlyBoxes;
|
361
|
+
};
|
362
|
+
|
363
|
+
if (!Array.prototype.forEach || !properlyBoxesContext(Array.prototype.forEach)) {
|
364
|
+
Array.prototype.forEach = function forEach(fun /*, thisp*/) {
|
365
|
+
var object = toObject(this),
|
366
|
+
self = splitString && _toString(this) == "[object String]" ?
|
367
|
+
this.split("") :
|
368
|
+
object,
|
369
|
+
thisp = arguments[1],
|
370
|
+
i = -1,
|
371
|
+
length = self.length >>> 0;
|
372
|
+
|
373
|
+
// If no callback function or if callback is not a callable function
|
374
|
+
if (_toString(fun) != "[object Function]") {
|
375
|
+
throw new TypeError(); // TODO message
|
376
|
+
}
|
377
|
+
|
378
|
+
while (++i < length) {
|
379
|
+
if (i in self) {
|
380
|
+
// Invoke the callback function with call, passing arguments:
|
381
|
+
// context, property value, property key, thisArg object
|
382
|
+
// context
|
383
|
+
fun.call(thisp, self[i], i, object);
|
384
|
+
}
|
385
|
+
}
|
386
|
+
};
|
387
|
+
}
|
388
|
+
|
389
|
+
// ES5 15.4.4.19
|
390
|
+
// http://es5.github.com/#x15.4.4.19
|
391
|
+
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map
|
392
|
+
if (!Array.prototype.map || !properlyBoxesContext(Array.prototype.map)) {
|
393
|
+
Array.prototype.map = function map(fun /*, thisp*/) {
|
394
|
+
var object = toObject(this),
|
395
|
+
self = splitString && _toString(this) == "[object String]" ?
|
396
|
+
this.split("") :
|
397
|
+
object,
|
398
|
+
length = self.length >>> 0,
|
399
|
+
result = Array(length),
|
400
|
+
thisp = arguments[1];
|
401
|
+
|
402
|
+
// If no callback function or if callback is not a callable function
|
403
|
+
if (_toString(fun) != "[object Function]") {
|
404
|
+
throw new TypeError(fun + " is not a function");
|
405
|
+
}
|
406
|
+
|
407
|
+
for (var i = 0; i < length; i++) {
|
408
|
+
if (i in self)
|
409
|
+
result[i] = fun.call(thisp, self[i], i, object);
|
410
|
+
}
|
411
|
+
return result;
|
412
|
+
};
|
413
|
+
}
|
414
|
+
|
415
|
+
// ES5 15.4.4.20
|
416
|
+
// http://es5.github.com/#x15.4.4.20
|
417
|
+
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter
|
418
|
+
if (!Array.prototype.filter || !properlyBoxesContext(Array.prototype.filter)) {
|
419
|
+
Array.prototype.filter = function filter(fun /*, thisp */) {
|
420
|
+
var object = toObject(this),
|
421
|
+
self = splitString && _toString(this) == "[object String]" ?
|
422
|
+
this.split("") :
|
423
|
+
object,
|
424
|
+
length = self.length >>> 0,
|
425
|
+
result = [],
|
426
|
+
value,
|
427
|
+
thisp = arguments[1];
|
428
|
+
|
429
|
+
// If no callback function or if callback is not a callable function
|
430
|
+
if (_toString(fun) != "[object Function]") {
|
431
|
+
throw new TypeError(fun + " is not a function");
|
432
|
+
}
|
433
|
+
|
434
|
+
for (var i = 0; i < length; i++) {
|
435
|
+
if (i in self) {
|
436
|
+
value = self[i];
|
437
|
+
if (fun.call(thisp, value, i, object)) {
|
438
|
+
result.push(value);
|
439
|
+
}
|
440
|
+
}
|
441
|
+
}
|
442
|
+
return result;
|
443
|
+
};
|
444
|
+
}
|
445
|
+
|
446
|
+
// ES5 15.4.4.16
|
447
|
+
// http://es5.github.com/#x15.4.4.16
|
448
|
+
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
|
449
|
+
if (!Array.prototype.every || !properlyBoxesContext(Array.prototype.every)) {
|
450
|
+
Array.prototype.every = function every(fun /*, thisp */) {
|
451
|
+
var object = toObject(this),
|
452
|
+
self = splitString && _toString(this) == "[object String]" ?
|
453
|
+
this.split("") :
|
454
|
+
object,
|
455
|
+
length = self.length >>> 0,
|
456
|
+
thisp = arguments[1];
|
457
|
+
|
458
|
+
// If no callback function or if callback is not a callable function
|
459
|
+
if (_toString(fun) != "[object Function]") {
|
460
|
+
throw new TypeError(fun + " is not a function");
|
461
|
+
}
|
462
|
+
|
463
|
+
for (var i = 0; i < length; i++) {
|
464
|
+
if (i in self && !fun.call(thisp, self[i], i, object)) {
|
465
|
+
return false;
|
466
|
+
}
|
467
|
+
}
|
468
|
+
return true;
|
469
|
+
};
|
470
|
+
}
|
471
|
+
|
472
|
+
// ES5 15.4.4.17
|
473
|
+
// http://es5.github.com/#x15.4.4.17
|
474
|
+
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
|
475
|
+
if (!Array.prototype.some || !properlyBoxesContext(Array.prototype.some)) {
|
476
|
+
Array.prototype.some = function some(fun /*, thisp */) {
|
477
|
+
var object = toObject(this),
|
478
|
+
self = splitString && _toString(this) == "[object String]" ?
|
479
|
+
this.split("") :
|
480
|
+
object,
|
481
|
+
length = self.length >>> 0,
|
482
|
+
thisp = arguments[1];
|
483
|
+
|
484
|
+
// If no callback function or if callback is not a callable function
|
485
|
+
if (_toString(fun) != "[object Function]") {
|
486
|
+
throw new TypeError(fun + " is not a function");
|
487
|
+
}
|
488
|
+
|
489
|
+
for (var i = 0; i < length; i++) {
|
490
|
+
if (i in self && fun.call(thisp, self[i], i, object)) {
|
491
|
+
return true;
|
492
|
+
}
|
493
|
+
}
|
494
|
+
return false;
|
495
|
+
};
|
496
|
+
}
|
497
|
+
|
498
|
+
// ES5 15.4.4.21
|
499
|
+
// http://es5.github.com/#x15.4.4.21
|
500
|
+
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduce
|
501
|
+
if (!Array.prototype.reduce) {
|
502
|
+
Array.prototype.reduce = function reduce(fun /*, initial*/) {
|
503
|
+
var object = toObject(this),
|
504
|
+
self = splitString && _toString(this) == "[object String]" ?
|
505
|
+
this.split("") :
|
506
|
+
object,
|
507
|
+
length = self.length >>> 0;
|
508
|
+
|
509
|
+
// If no callback function or if callback is not a callable function
|
510
|
+
if (_toString(fun) != "[object Function]") {
|
511
|
+
throw new TypeError(fun + " is not a function");
|
512
|
+
}
|
513
|
+
|
514
|
+
// no value to return if no initial value and an empty array
|
515
|
+
if (!length && arguments.length == 1) {
|
516
|
+
throw new TypeError("reduce of empty array with no initial value");
|
517
|
+
}
|
518
|
+
|
519
|
+
var i = 0;
|
520
|
+
var result;
|
521
|
+
if (arguments.length >= 2) {
|
522
|
+
result = arguments[1];
|
523
|
+
} else {
|
524
|
+
do {
|
525
|
+
if (i in self) {
|
526
|
+
result = self[i++];
|
527
|
+
break;
|
528
|
+
}
|
529
|
+
|
530
|
+
// if array contains no values, no initial value to return
|
531
|
+
if (++i >= length) {
|
532
|
+
throw new TypeError("reduce of empty array with no initial value");
|
533
|
+
}
|
534
|
+
} while (true);
|
535
|
+
}
|
536
|
+
|
537
|
+
for (; i < length; i++) {
|
538
|
+
if (i in self) {
|
539
|
+
result = fun.call(void 0, result, self[i], i, object);
|
540
|
+
}
|
541
|
+
}
|
542
|
+
|
543
|
+
return result;
|
544
|
+
};
|
545
|
+
}
|
546
|
+
|
547
|
+
// ES5 15.4.4.22
|
548
|
+
// http://es5.github.com/#x15.4.4.22
|
549
|
+
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduceRight
|
550
|
+
if (!Array.prototype.reduceRight) {
|
551
|
+
Array.prototype.reduceRight = function reduceRight(fun /*, initial*/) {
|
552
|
+
var object = toObject(this),
|
553
|
+
self = splitString && _toString(this) == "[object String]" ?
|
554
|
+
this.split("") :
|
555
|
+
object,
|
556
|
+
length = self.length >>> 0;
|
557
|
+
|
558
|
+
// If no callback function or if callback is not a callable function
|
559
|
+
if (_toString(fun) != "[object Function]") {
|
560
|
+
throw new TypeError(fun + " is not a function");
|
561
|
+
}
|
562
|
+
|
563
|
+
// no value to return if no initial value, empty array
|
564
|
+
if (!length && arguments.length == 1) {
|
565
|
+
throw new TypeError("reduceRight of empty array with no initial value");
|
566
|
+
}
|
567
|
+
|
568
|
+
var result, i = length - 1;
|
569
|
+
if (arguments.length >= 2) {
|
570
|
+
result = arguments[1];
|
571
|
+
} else {
|
572
|
+
do {
|
573
|
+
if (i in self) {
|
574
|
+
result = self[i--];
|
575
|
+
break;
|
576
|
+
}
|
577
|
+
|
578
|
+
// if array contains no values, no initial value to return
|
579
|
+
if (--i < 0) {
|
580
|
+
throw new TypeError("reduceRight of empty array with no initial value");
|
581
|
+
}
|
582
|
+
} while (true);
|
583
|
+
}
|
584
|
+
|
585
|
+
if (i < 0) {
|
586
|
+
return result;
|
587
|
+
}
|
588
|
+
|
589
|
+
do {
|
590
|
+
if (i in this) {
|
591
|
+
result = fun.call(void 0, result, self[i], i, object);
|
592
|
+
}
|
593
|
+
} while (i--);
|
594
|
+
|
595
|
+
return result;
|
596
|
+
};
|
597
|
+
}
|
598
|
+
|
599
|
+
// ES5 15.4.4.14
|
600
|
+
// http://es5.github.com/#x15.4.4.14
|
601
|
+
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
|
602
|
+
if (!Array.prototype.indexOf || ([0, 1].indexOf(1, 2) != -1)) {
|
603
|
+
Array.prototype.indexOf = function indexOf(sought /*, fromIndex */ ) {
|
604
|
+
var self = splitString && _toString(this) == "[object String]" ?
|
605
|
+
this.split("") :
|
606
|
+
toObject(this),
|
607
|
+
length = self.length >>> 0;
|
608
|
+
|
609
|
+
if (!length) {
|
610
|
+
return -1;
|
611
|
+
}
|
612
|
+
|
613
|
+
var i = 0;
|
614
|
+
if (arguments.length > 1) {
|
615
|
+
i = toInteger(arguments[1]);
|
616
|
+
}
|
617
|
+
|
618
|
+
// handle negative indices
|
619
|
+
i = i >= 0 ? i : Math.max(0, length + i);
|
620
|
+
for (; i < length; i++) {
|
621
|
+
if (i in self && self[i] === sought) {
|
622
|
+
return i;
|
623
|
+
}
|
624
|
+
}
|
625
|
+
return -1;
|
626
|
+
};
|
627
|
+
}
|
628
|
+
|
629
|
+
// ES5 15.4.4.15
|
630
|
+
// http://es5.github.com/#x15.4.4.15
|
631
|
+
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf
|
632
|
+
if (!Array.prototype.lastIndexOf || ([0, 1].lastIndexOf(0, -3) != -1)) {
|
633
|
+
Array.prototype.lastIndexOf = function lastIndexOf(sought /*, fromIndex */) {
|
634
|
+
var self = splitString && _toString(this) == "[object String]" ?
|
635
|
+
this.split("") :
|
636
|
+
toObject(this),
|
637
|
+
length = self.length >>> 0;
|
638
|
+
|
639
|
+
if (!length) {
|
640
|
+
return -1;
|
641
|
+
}
|
642
|
+
var i = length - 1;
|
643
|
+
if (arguments.length > 1) {
|
644
|
+
i = Math.min(i, toInteger(arguments[1]));
|
645
|
+
}
|
646
|
+
// handle negative indices
|
647
|
+
i = i >= 0 ? i : length - Math.abs(i);
|
648
|
+
for (; i >= 0; i--) {
|
649
|
+
if (i in self && sought === self[i]) {
|
650
|
+
return i;
|
651
|
+
}
|
652
|
+
}
|
653
|
+
return -1;
|
654
|
+
};
|
655
|
+
}
|
656
|
+
|
657
|
+
//
|
658
|
+
// Object
|
659
|
+
// ======
|
660
|
+
//
|
661
|
+
|
662
|
+
// ES5 15.2.3.14
|
663
|
+
// http://es5.github.com/#x15.2.3.14
|
664
|
+
if (!Object.keys) {
|
665
|
+
// http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
|
666
|
+
var hasDontEnumBug = !({'toString': null}).propertyIsEnumerable('toString'),
|
667
|
+
hasProtoEnumBug = (function () {}).propertyIsEnumerable('prototype'),
|
668
|
+
dontEnums = [
|
669
|
+
"toString",
|
670
|
+
"toLocaleString",
|
671
|
+
"valueOf",
|
672
|
+
"hasOwnProperty",
|
673
|
+
"isPrototypeOf",
|
674
|
+
"propertyIsEnumerable",
|
675
|
+
"constructor"
|
676
|
+
],
|
677
|
+
dontEnumsLength = dontEnums.length,
|
678
|
+
isFunction = function isFunction(value) {
|
679
|
+
return _toString(value) === '[object Function]';
|
680
|
+
},
|
681
|
+
isArguments = function isArguments(value) {
|
682
|
+
var str = _toString(value);
|
683
|
+
var isArguments = str === '[object Arguments]';
|
684
|
+
if (!isArguments) {
|
685
|
+
isArguments = !Array.isArray(str)
|
686
|
+
&& value !== null
|
687
|
+
&& typeof value === 'object'
|
688
|
+
&& typeof value.length === 'number'
|
689
|
+
&& value.length >= 0
|
690
|
+
&& isFunction(value.callee);
|
691
|
+
}
|
692
|
+
return isArguments;
|
693
|
+
};
|
694
|
+
|
695
|
+
Object.keys = function keys(object) {
|
696
|
+
var isFunction = isFunction(object),
|
697
|
+
isArguments = isArguments(object),
|
698
|
+
isObject = object !== null && typeof object === 'object';
|
699
|
+
|
700
|
+
if (!isObject && !isFunction) {
|
701
|
+
throw new TypeError("Object.keys called on a non-object");
|
702
|
+
}
|
703
|
+
|
704
|
+
var keys = [],
|
705
|
+
skipProto = hasProtoEnumBug && isFunction;
|
706
|
+
for (var name in object) {
|
707
|
+
if (!(skipProto && name === 'prototype') && owns(object, name)) {
|
708
|
+
keys.push(name);
|
709
|
+
}
|
710
|
+
}
|
711
|
+
|
712
|
+
if (hasDontEnumBug) {
|
713
|
+
var ctor = object.constructor,
|
714
|
+
skipConstructor = ctor && ctor.prototype === object;
|
715
|
+
for (var i = 0; i < dontEnumsLength; i++) {
|
716
|
+
var dontEnum = dontEnums[i];
|
717
|
+
if (!(skipConstructor && dontEnum === 'constructor') && owns(object, dontEnum)) {
|
718
|
+
keys.push(dontEnum);
|
719
|
+
}
|
720
|
+
}
|
721
|
+
}
|
722
|
+
return keys;
|
723
|
+
};
|
724
|
+
|
725
|
+
}
|
726
|
+
|
727
|
+
//
|
728
|
+
// Date
|
729
|
+
// ====
|
730
|
+
//
|
731
|
+
|
732
|
+
// ES5 15.9.5.43
|
733
|
+
// http://es5.github.com/#x15.9.5.43
|
734
|
+
// This function returns a String value represent the instance in time
|
735
|
+
// represented by this Date object. The format of the String is the Date Time
|
736
|
+
// string format defined in 15.9.1.15. All fields are present in the String.
|
737
|
+
// The time zone is always UTC, denoted by the suffix Z. If the time value of
|
738
|
+
// this object is not a finite Number a RangeError exception is thrown.
|
739
|
+
var negativeDate = -62198755200000,
|
740
|
+
negativeYearString = "-000001";
|
741
|
+
if (
|
742
|
+
!Date.prototype.toISOString ||
|
743
|
+
(new Date(negativeDate).toISOString().indexOf(negativeYearString) === -1)
|
744
|
+
) {
|
745
|
+
Date.prototype.toISOString = function toISOString() {
|
746
|
+
var result, length, value, year, month;
|
747
|
+
if (!isFinite(this)) {
|
748
|
+
throw new RangeError("Date.prototype.toISOString called on non-finite value.");
|
749
|
+
}
|
750
|
+
|
751
|
+
year = this.getUTCFullYear();
|
752
|
+
|
753
|
+
month = this.getUTCMonth();
|
754
|
+
// see https://github.com/es-shims/es5-shim/issues/111
|
755
|
+
year += Math.floor(month / 12);
|
756
|
+
month = (month % 12 + 12) % 12;
|
757
|
+
|
758
|
+
// the date time string format is specified in 15.9.1.15.
|
759
|
+
result = [month + 1, this.getUTCDate(),
|
760
|
+
this.getUTCHours(), this.getUTCMinutes(), this.getUTCSeconds()];
|
761
|
+
year = (
|
762
|
+
(year < 0 ? "-" : (year > 9999 ? "+" : "")) +
|
763
|
+
("00000" + Math.abs(year))
|
764
|
+
.slice(0 <= year && year <= 9999 ? -4 : -6)
|
765
|
+
);
|
766
|
+
|
767
|
+
length = result.length;
|
768
|
+
while (length--) {
|
769
|
+
value = result[length];
|
770
|
+
// pad months, days, hours, minutes, and seconds to have two
|
771
|
+
// digits.
|
772
|
+
if (value < 10) {
|
773
|
+
result[length] = "0" + value;
|
774
|
+
}
|
775
|
+
}
|
776
|
+
// pad milliseconds to have three digits.
|
777
|
+
return (
|
778
|
+
year + "-" + result.slice(0, 2).join("-") +
|
779
|
+
"T" + result.slice(2).join(":") + "." +
|
780
|
+
("000" + this.getUTCMilliseconds()).slice(-3) + "Z"
|
781
|
+
);
|
782
|
+
};
|
783
|
+
}
|
784
|
+
|
785
|
+
|
786
|
+
// ES5 15.9.5.44
|
787
|
+
// http://es5.github.com/#x15.9.5.44
|
788
|
+
// This function provides a String representation of a Date object for use by
|
789
|
+
// JSON.stringify (15.12.3).
|
790
|
+
var dateToJSONIsSupported = false;
|
791
|
+
try {
|
792
|
+
dateToJSONIsSupported = (
|
793
|
+
Date.prototype.toJSON &&
|
794
|
+
new Date(NaN).toJSON() === null &&
|
795
|
+
new Date(negativeDate).toJSON().indexOf(negativeYearString) !== -1 &&
|
796
|
+
Date.prototype.toJSON.call({ // generic
|
797
|
+
toISOString: function () {
|
798
|
+
return true;
|
799
|
+
}
|
800
|
+
})
|
801
|
+
);
|
802
|
+
} catch (e) {
|
803
|
+
}
|
804
|
+
if (!dateToJSONIsSupported) {
|
805
|
+
Date.prototype.toJSON = function toJSON(key) {
|
806
|
+
// When the toJSON method is called with argument key, the following
|
807
|
+
// steps are taken:
|
808
|
+
|
809
|
+
// 1. Let O be the result of calling ToObject, giving it the this
|
810
|
+
// value as its argument.
|
811
|
+
// 2. Let tv be toPrimitive(O, hint Number).
|
812
|
+
var o = Object(this),
|
813
|
+
tv = toPrimitive(o),
|
814
|
+
toISO;
|
815
|
+
// 3. If tv is a Number and is not finite, return null.
|
816
|
+
if (typeof tv === "number" && !isFinite(tv)) {
|
817
|
+
return null;
|
818
|
+
}
|
819
|
+
// 4. Let toISO be the result of calling the [[Get]] internal method of
|
820
|
+
// O with argument "toISOString".
|
821
|
+
toISO = o.toISOString;
|
822
|
+
// 5. If IsCallable(toISO) is false, throw a TypeError exception.
|
823
|
+
if (typeof toISO != "function") {
|
824
|
+
throw new TypeError("toISOString property is not callable");
|
825
|
+
}
|
826
|
+
// 6. Return the result of calling the [[Call]] internal method of
|
827
|
+
// toISO with O as the this value and an empty argument list.
|
828
|
+
return toISO.call(o);
|
829
|
+
|
830
|
+
// NOTE 1 The argument is ignored.
|
831
|
+
|
832
|
+
// NOTE 2 The toJSON function is intentionally generic; it does not
|
833
|
+
// require that its this value be a Date object. Therefore, it can be
|
834
|
+
// transferred to other kinds of objects for use as a method. However,
|
835
|
+
// it does require that any such object have a toISOString method. An
|
836
|
+
// object is free to use the argument key to filter its
|
837
|
+
// stringification.
|
838
|
+
};
|
839
|
+
}
|
840
|
+
|
841
|
+
// ES5 15.9.4.2
|
842
|
+
// http://es5.github.com/#x15.9.4.2
|
843
|
+
// based on work shared by Daniel Friesen (dantman)
|
844
|
+
// http://gist.github.com/303249
|
845
|
+
if (!Date.parse || isNaN(Date.parse("2000-01-01T00:00:00.000Z"))) {
|
846
|
+
// XXX global assignment won't work in embeddings that use
|
847
|
+
// an alternate object for the context.
|
848
|
+
Date = (function(NativeDate) {
|
849
|
+
|
850
|
+
// Date.length === 7
|
851
|
+
function Date(Y, M, D, h, m, s, ms) {
|
852
|
+
var length = arguments.length;
|
853
|
+
if (this instanceof NativeDate) {
|
854
|
+
var date = length == 1 && String(Y) === Y ? // isString(Y)
|
855
|
+
// We explicitly pass it through parse:
|
856
|
+
new NativeDate(Date.parse(Y)) :
|
857
|
+
// We have to manually make calls depending on argument
|
858
|
+
// length here
|
859
|
+
length >= 7 ? new NativeDate(Y, M, D, h, m, s, ms) :
|
860
|
+
length >= 6 ? new NativeDate(Y, M, D, h, m, s) :
|
861
|
+
length >= 5 ? new NativeDate(Y, M, D, h, m) :
|
862
|
+
length >= 4 ? new NativeDate(Y, M, D, h) :
|
863
|
+
length >= 3 ? new NativeDate(Y, M, D) :
|
864
|
+
length >= 2 ? new NativeDate(Y, M) :
|
865
|
+
length >= 1 ? new NativeDate(Y) :
|
866
|
+
new NativeDate();
|
867
|
+
// Prevent mixups with unfixed Date object
|
868
|
+
date.constructor = Date;
|
869
|
+
return date;
|
870
|
+
}
|
871
|
+
return NativeDate.apply(this, arguments);
|
872
|
+
};
|
873
|
+
|
874
|
+
// 15.9.1.15 Date Time String Format.
|
875
|
+
var isoDateExpression = new RegExp("^" +
|
876
|
+
"(\\d{4}|[\+\-]\\d{6})" + // four-digit year capture or sign +
|
877
|
+
// 6-digit extended year
|
878
|
+
"(?:-(\\d{2})" + // optional month capture
|
879
|
+
"(?:-(\\d{2})" + // optional day capture
|
880
|
+
"(?:" + // capture hours:minutes:seconds.milliseconds
|
881
|
+
"T(\\d{2})" + // hours capture
|
882
|
+
":(\\d{2})" + // minutes capture
|
883
|
+
"(?:" + // optional :seconds.milliseconds
|
884
|
+
":(\\d{2})" + // seconds capture
|
885
|
+
"(?:(\\.\\d{1,}))?" + // milliseconds capture
|
886
|
+
")?" +
|
887
|
+
"(" + // capture UTC offset component
|
888
|
+
"Z|" + // UTC capture
|
889
|
+
"(?:" + // offset specifier +/-hours:minutes
|
890
|
+
"([-+])" + // sign capture
|
891
|
+
"(\\d{2})" + // hours offset capture
|
892
|
+
":(\\d{2})" + // minutes offset capture
|
893
|
+
")" +
|
894
|
+
")?)?)?)?" +
|
895
|
+
"$");
|
896
|
+
|
897
|
+
var months = [
|
898
|
+
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
|
899
|
+
];
|
900
|
+
|
901
|
+
function dayFromMonth(year, month) {
|
902
|
+
var t = month > 1 ? 1 : 0;
|
903
|
+
return (
|
904
|
+
months[month] +
|
905
|
+
Math.floor((year - 1969 + t) / 4) -
|
906
|
+
Math.floor((year - 1901 + t) / 100) +
|
907
|
+
Math.floor((year - 1601 + t) / 400) +
|
908
|
+
365 * (year - 1970)
|
909
|
+
);
|
910
|
+
}
|
911
|
+
|
912
|
+
function toUTC(t) {
|
913
|
+
return Number(new NativeDate(1970, 0, 1, 0, 0, 0, t));
|
914
|
+
}
|
915
|
+
|
916
|
+
// Copy any custom methods a 3rd party library may have added
|
917
|
+
for (var key in NativeDate) {
|
918
|
+
Date[key] = NativeDate[key];
|
919
|
+
}
|
920
|
+
|
921
|
+
// Copy "native" methods explicitly; they may be non-enumerable
|
922
|
+
Date.now = NativeDate.now;
|
923
|
+
Date.UTC = NativeDate.UTC;
|
924
|
+
Date.prototype = NativeDate.prototype;
|
925
|
+
Date.prototype.constructor = Date;
|
926
|
+
|
927
|
+
// Upgrade Date.parse to handle simplified ISO 8601 strings
|
928
|
+
Date.parse = function parse(string) {
|
929
|
+
var match = isoDateExpression.exec(string);
|
930
|
+
if (match) {
|
931
|
+
// parse months, days, hours, minutes, seconds, and milliseconds
|
932
|
+
// provide default values if necessary
|
933
|
+
// parse the UTC offset component
|
934
|
+
var year = Number(match[1]),
|
935
|
+
month = Number(match[2] || 1) - 1,
|
936
|
+
day = Number(match[3] || 1) - 1,
|
937
|
+
hour = Number(match[4] || 0),
|
938
|
+
minute = Number(match[5] || 0),
|
939
|
+
second = Number(match[6] || 0),
|
940
|
+
millisecond = Math.floor(Number(match[7] || 0) * 1000),
|
941
|
+
// When time zone is missed, local offset should be used
|
942
|
+
// (ES 5.1 bug)
|
943
|
+
// see https://bugs.ecmascript.org/show_bug.cgi?id=112
|
944
|
+
isLocalTime = Boolean(match[4] && !match[8]),
|
945
|
+
signOffset = match[9] === "-" ? 1 : -1,
|
946
|
+
hourOffset = Number(match[10] || 0),
|
947
|
+
minuteOffset = Number(match[11] || 0),
|
948
|
+
result;
|
949
|
+
if (
|
950
|
+
hour < (
|
951
|
+
minute > 0 || second > 0 || millisecond > 0 ?
|
952
|
+
24 : 25
|
953
|
+
) &&
|
954
|
+
minute < 60 && second < 60 && millisecond < 1000 &&
|
955
|
+
month > -1 && month < 12 && hourOffset < 24 &&
|
956
|
+
minuteOffset < 60 && // detect invalid offsets
|
957
|
+
day > -1 &&
|
958
|
+
day < (
|
959
|
+
dayFromMonth(year, month + 1) -
|
960
|
+
dayFromMonth(year, month)
|
961
|
+
)
|
962
|
+
) {
|
963
|
+
result = (
|
964
|
+
(dayFromMonth(year, month) + day) * 24 +
|
965
|
+
hour +
|
966
|
+
hourOffset * signOffset
|
967
|
+
) * 60;
|
968
|
+
result = (
|
969
|
+
(result + minute + minuteOffset * signOffset) * 60 +
|
970
|
+
second
|
971
|
+
) * 1000 + millisecond;
|
972
|
+
if (isLocalTime) {
|
973
|
+
result = toUTC(result);
|
974
|
+
}
|
975
|
+
if (-8.64e15 <= result && result <= 8.64e15) {
|
976
|
+
return result;
|
977
|
+
}
|
978
|
+
}
|
979
|
+
return NaN;
|
980
|
+
}
|
981
|
+
return NativeDate.parse.apply(this, arguments);
|
982
|
+
};
|
983
|
+
|
984
|
+
return Date;
|
985
|
+
})(Date);
|
986
|
+
}
|
987
|
+
|
988
|
+
// ES5 15.9.4.4
|
989
|
+
// http://es5.github.com/#x15.9.4.4
|
990
|
+
if (!Date.now) {
|
991
|
+
Date.now = function now() {
|
992
|
+
return new Date().getTime();
|
993
|
+
};
|
994
|
+
}
|
995
|
+
|
996
|
+
|
997
|
+
//
|
998
|
+
// Number
|
999
|
+
// ======
|
1000
|
+
//
|
1001
|
+
|
1002
|
+
// ES5.1 15.7.4.5
|
1003
|
+
// http://es5.github.com/#x15.7.4.5
|
1004
|
+
if (!Number.prototype.toFixed || (0.00008).toFixed(3) !== '0.000' || (0.9).toFixed(0) === '0' || (1.255).toFixed(2) !== '1.25' || (1000000000000000128).toFixed(0) !== "1000000000000000128") {
|
1005
|
+
// Hide these variables and functions
|
1006
|
+
(function () {
|
1007
|
+
var base, size, data, i;
|
1008
|
+
|
1009
|
+
base = 1e7;
|
1010
|
+
size = 6;
|
1011
|
+
data = [0, 0, 0, 0, 0, 0];
|
1012
|
+
|
1013
|
+
function multiply(n, c) {
|
1014
|
+
var i = -1;
|
1015
|
+
while (++i < size) {
|
1016
|
+
c += n * data[i];
|
1017
|
+
data[i] = c % base;
|
1018
|
+
c = Math.floor(c / base);
|
1019
|
+
}
|
1020
|
+
}
|
1021
|
+
|
1022
|
+
function divide(n) {
|
1023
|
+
var i = size, c = 0;
|
1024
|
+
while (--i >= 0) {
|
1025
|
+
c += data[i];
|
1026
|
+
data[i] = Math.floor(c / n);
|
1027
|
+
c = (c % n) * base;
|
1028
|
+
}
|
1029
|
+
}
|
1030
|
+
|
1031
|
+
function toString() {
|
1032
|
+
var i = size;
|
1033
|
+
var s = '';
|
1034
|
+
while (--i >= 0) {
|
1035
|
+
if (s !== '' || i === 0 || data[i] !== 0) {
|
1036
|
+
var t = String(data[i]);
|
1037
|
+
if (s === '') {
|
1038
|
+
s = t;
|
1039
|
+
} else {
|
1040
|
+
s += '0000000'.slice(0, 7 - t.length) + t;
|
1041
|
+
}
|
1042
|
+
}
|
1043
|
+
}
|
1044
|
+
return s;
|
1045
|
+
}
|
1046
|
+
|
1047
|
+
function pow(x, n, acc) {
|
1048
|
+
return (n === 0 ? acc : (n % 2 === 1 ? pow(x, n - 1, acc * x) : pow(x * x, n / 2, acc)));
|
1049
|
+
}
|
1050
|
+
|
1051
|
+
function log(x) {
|
1052
|
+
var n = 0;
|
1053
|
+
while (x >= 4096) {
|
1054
|
+
n += 12;
|
1055
|
+
x /= 4096;
|
1056
|
+
}
|
1057
|
+
while (x >= 2) {
|
1058
|
+
n += 1;
|
1059
|
+
x /= 2;
|
1060
|
+
}
|
1061
|
+
return n;
|
1062
|
+
}
|
1063
|
+
|
1064
|
+
Number.prototype.toFixed = function (fractionDigits) {
|
1065
|
+
var f, x, s, m, e, z, j, k;
|
1066
|
+
|
1067
|
+
// Test for NaN and round fractionDigits down
|
1068
|
+
f = Number(fractionDigits);
|
1069
|
+
f = f !== f ? 0 : Math.floor(f);
|
1070
|
+
|
1071
|
+
if (f < 0 || f > 20) {
|
1072
|
+
throw new RangeError("Number.toFixed called with invalid number of decimals");
|
1073
|
+
}
|
1074
|
+
|
1075
|
+
x = Number(this);
|
1076
|
+
|
1077
|
+
// Test for NaN
|
1078
|
+
if (x !== x) {
|
1079
|
+
return "NaN";
|
1080
|
+
}
|
1081
|
+
|
1082
|
+
// If it is too big or small, return the string value of the number
|
1083
|
+
if (x <= -1e21 || x >= 1e21) {
|
1084
|
+
return String(x);
|
1085
|
+
}
|
1086
|
+
|
1087
|
+
s = "";
|
1088
|
+
|
1089
|
+
if (x < 0) {
|
1090
|
+
s = "-";
|
1091
|
+
x = -x;
|
1092
|
+
}
|
1093
|
+
|
1094
|
+
m = "0";
|
1095
|
+
|
1096
|
+
if (x > 1e-21) {
|
1097
|
+
// 1e-21 < x < 1e21
|
1098
|
+
// -70 < log2(x) < 70
|
1099
|
+
e = log(x * pow(2, 69, 1)) - 69;
|
1100
|
+
z = (e < 0 ? x * pow(2, -e, 1) : x / pow(2, e, 1));
|
1101
|
+
z *= 0x10000000000000; // Math.pow(2, 52);
|
1102
|
+
e = 52 - e;
|
1103
|
+
|
1104
|
+
// -18 < e < 122
|
1105
|
+
// x = z / 2 ^ e
|
1106
|
+
if (e > 0) {
|
1107
|
+
multiply(0, z);
|
1108
|
+
j = f;
|
1109
|
+
|
1110
|
+
while (j >= 7) {
|
1111
|
+
multiply(1e7, 0);
|
1112
|
+
j -= 7;
|
1113
|
+
}
|
1114
|
+
|
1115
|
+
multiply(pow(10, j, 1), 0);
|
1116
|
+
j = e - 1;
|
1117
|
+
|
1118
|
+
while (j >= 23) {
|
1119
|
+
divide(1 << 23);
|
1120
|
+
j -= 23;
|
1121
|
+
}
|
1122
|
+
|
1123
|
+
divide(1 << j);
|
1124
|
+
multiply(1, 1);
|
1125
|
+
divide(2);
|
1126
|
+
m = toString();
|
1127
|
+
} else {
|
1128
|
+
multiply(0, z);
|
1129
|
+
multiply(1 << (-e), 0);
|
1130
|
+
m = toString() + '0.00000000000000000000'.slice(2, 2 + f);
|
1131
|
+
}
|
1132
|
+
}
|
1133
|
+
|
1134
|
+
if (f > 0) {
|
1135
|
+
k = m.length;
|
1136
|
+
|
1137
|
+
if (k <= f) {
|
1138
|
+
m = s + '0.0000000000000000000'.slice(0, f - k + 2) + m;
|
1139
|
+
} else {
|
1140
|
+
m = s + m.slice(0, k - f) + '.' + m.slice(k - f);
|
1141
|
+
}
|
1142
|
+
} else {
|
1143
|
+
m = s + m;
|
1144
|
+
}
|
1145
|
+
|
1146
|
+
return m;
|
1147
|
+
}
|
1148
|
+
}());
|
1149
|
+
}
|
1150
|
+
|
1151
|
+
|
1152
|
+
//
|
1153
|
+
// String
|
1154
|
+
// ======
|
1155
|
+
//
|
1156
|
+
|
1157
|
+
|
1158
|
+
// ES5 15.5.4.14
|
1159
|
+
// http://es5.github.com/#x15.5.4.14
|
1160
|
+
|
1161
|
+
// [bugfix, IE lt 9, firefox 4, Konqueror, Opera, obscure browsers]
|
1162
|
+
// Many browsers do not split properly with regular expressions or they
|
1163
|
+
// do not perform the split correctly under obscure conditions.
|
1164
|
+
// See http://blog.stevenlevithan.com/archives/cross-browser-split
|
1165
|
+
// I've tested in many browsers and this seems to cover the deviant ones:
|
1166
|
+
// 'ab'.split(/(?:ab)*/) should be ["", ""], not [""]
|
1167
|
+
// '.'.split(/(.?)(.?)/) should be ["", ".", "", ""], not ["", ""]
|
1168
|
+
// 'tesst'.split(/(s)*/) should be ["t", undefined, "e", "s", "t"], not
|
1169
|
+
// [undefined, "t", undefined, "e", ...]
|
1170
|
+
// ''.split(/.?/) should be [], not [""]
|
1171
|
+
// '.'.split(/()()/) should be ["."], not ["", "", "."]
|
1172
|
+
|
1173
|
+
var string_split = String.prototype.split;
|
1174
|
+
if (
|
1175
|
+
'ab'.split(/(?:ab)*/).length !== 2 ||
|
1176
|
+
'.'.split(/(.?)(.?)/).length !== 4 ||
|
1177
|
+
'tesst'.split(/(s)*/)[1] === "t" ||
|
1178
|
+
''.split(/.?/).length ||
|
1179
|
+
'.'.split(/()()/).length > 1
|
1180
|
+
) {
|
1181
|
+
(function () {
|
1182
|
+
var compliantExecNpcg = /()??/.exec("")[1] === void 0; // NPCG: nonparticipating capturing group
|
1183
|
+
|
1184
|
+
String.prototype.split = function (separator, limit) {
|
1185
|
+
var string = this;
|
1186
|
+
if (separator === void 0 && limit === 0)
|
1187
|
+
return [];
|
1188
|
+
|
1189
|
+
// If `separator` is not a regex, use native split
|
1190
|
+
if (Object.prototype.toString.call(separator) !== "[object RegExp]") {
|
1191
|
+
return string_split.apply(this, arguments);
|
1192
|
+
}
|
1193
|
+
|
1194
|
+
var output = [],
|
1195
|
+
flags = (separator.ignoreCase ? "i" : "") +
|
1196
|
+
(separator.multiline ? "m" : "") +
|
1197
|
+
(separator.extended ? "x" : "") + // Proposed for ES6
|
1198
|
+
(separator.sticky ? "y" : ""), // Firefox 3+
|
1199
|
+
lastLastIndex = 0,
|
1200
|
+
// Make `global` and avoid `lastIndex` issues by working with a copy
|
1201
|
+
separator = new RegExp(separator.source, flags + "g"),
|
1202
|
+
separator2, match, lastIndex, lastLength;
|
1203
|
+
string += ""; // Type-convert
|
1204
|
+
if (!compliantExecNpcg) {
|
1205
|
+
// Doesn't need flags gy, but they don't hurt
|
1206
|
+
separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags);
|
1207
|
+
}
|
1208
|
+
/* Values for `limit`, per the spec:
|
1209
|
+
* If undefined: 4294967295 // Math.pow(2, 32) - 1
|
1210
|
+
* If 0, Infinity, or NaN: 0
|
1211
|
+
* If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
|
1212
|
+
* If negative number: 4294967296 - Math.floor(Math.abs(limit))
|
1213
|
+
* If other: Type-convert, then use the above rules
|
1214
|
+
*/
|
1215
|
+
limit = limit === void 0 ?
|
1216
|
+
-1 >>> 0 : // Math.pow(2, 32) - 1
|
1217
|
+
limit >>> 0; // ToUint32(limit)
|
1218
|
+
while (match = separator.exec(string)) {
|
1219
|
+
// `separator.lastIndex` is not reliable cross-browser
|
1220
|
+
lastIndex = match.index + match[0].length;
|
1221
|
+
if (lastIndex > lastLastIndex) {
|
1222
|
+
output.push(string.slice(lastLastIndex, match.index));
|
1223
|
+
// Fix browsers whose `exec` methods don't consistently return `undefined` for
|
1224
|
+
// nonparticipating capturing groups
|
1225
|
+
if (!compliantExecNpcg && match.length > 1) {
|
1226
|
+
match[0].replace(separator2, function () {
|
1227
|
+
for (var i = 1; i < arguments.length - 2; i++) {
|
1228
|
+
if (arguments[i] === void 0) {
|
1229
|
+
match[i] = void 0;
|
1230
|
+
}
|
1231
|
+
}
|
1232
|
+
});
|
1233
|
+
}
|
1234
|
+
if (match.length > 1 && match.index < string.length) {
|
1235
|
+
Array.prototype.push.apply(output, match.slice(1));
|
1236
|
+
}
|
1237
|
+
lastLength = match[0].length;
|
1238
|
+
lastLastIndex = lastIndex;
|
1239
|
+
if (output.length >= limit) {
|
1240
|
+
break;
|
1241
|
+
}
|
1242
|
+
}
|
1243
|
+
if (separator.lastIndex === match.index) {
|
1244
|
+
separator.lastIndex++; // Avoid an infinite loop
|
1245
|
+
}
|
1246
|
+
}
|
1247
|
+
if (lastLastIndex === string.length) {
|
1248
|
+
if (lastLength || !separator.test("")) {
|
1249
|
+
output.push("");
|
1250
|
+
}
|
1251
|
+
} else {
|
1252
|
+
output.push(string.slice(lastLastIndex));
|
1253
|
+
}
|
1254
|
+
return output.length > limit ? output.slice(0, limit) : output;
|
1255
|
+
};
|
1256
|
+
}());
|
1257
|
+
|
1258
|
+
// [bugfix, chrome]
|
1259
|
+
// If separator is undefined, then the result array contains just one String,
|
1260
|
+
// which is the this value (converted to a String). If limit is not undefined,
|
1261
|
+
// then the output array is truncated so that it contains no more than limit
|
1262
|
+
// elements.
|
1263
|
+
// "0".split(undefined, 0) -> []
|
1264
|
+
} else if ("0".split(void 0, 0).length) {
|
1265
|
+
String.prototype.split = function(separator, limit) {
|
1266
|
+
if (separator === void 0 && limit === 0) return [];
|
1267
|
+
return string_split.apply(this, arguments);
|
1268
|
+
}
|
1269
|
+
}
|
1270
|
+
|
1271
|
+
|
1272
|
+
// ECMA-262, 3rd B.2.3
|
1273
|
+
// Note an ECMAScript standart, although ECMAScript 3rd Edition has a
|
1274
|
+
// non-normative section suggesting uniform semantics and it should be
|
1275
|
+
// normalized across all browsers
|
1276
|
+
// [bugfix, IE lt 9] IE < 9 substr() with negative value not working in IE
|
1277
|
+
if ("".substr && "0b".substr(-1) !== "b") {
|
1278
|
+
var string_substr = String.prototype.substr;
|
1279
|
+
/**
|
1280
|
+
* Get the substring of a string
|
1281
|
+
* @param {integer} start where to start the substring
|
1282
|
+
* @param {integer} length how many characters to return
|
1283
|
+
* @return {string}
|
1284
|
+
*/
|
1285
|
+
String.prototype.substr = function(start, length) {
|
1286
|
+
return string_substr.call(
|
1287
|
+
this,
|
1288
|
+
start < 0 ? ((start = this.length + start) < 0 ? 0 : start) : start,
|
1289
|
+
length
|
1290
|
+
);
|
1291
|
+
}
|
1292
|
+
}
|
1293
|
+
|
1294
|
+
// ES5 15.5.4.20
|
1295
|
+
// whitespace from: http://es5.github.io/#x15.5.4.20
|
1296
|
+
var ws = "\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003" +
|
1297
|
+
"\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028" +
|
1298
|
+
"\u2029\uFEFF";
|
1299
|
+
if (!String.prototype.trim || ws.trim()) {
|
1300
|
+
// http://blog.stevenlevithan.com/archives/faster-trim-javascript
|
1301
|
+
// http://perfectionkills.com/whitespace-deviations/
|
1302
|
+
ws = "[" + ws + "]";
|
1303
|
+
var trimBeginRegexp = new RegExp("^" + ws + ws + "*"),
|
1304
|
+
trimEndRegexp = new RegExp(ws + ws + "*$");
|
1305
|
+
String.prototype.trim = function trim() {
|
1306
|
+
if (this === void 0 || this === null) {
|
1307
|
+
throw new TypeError("can't convert "+this+" to object");
|
1308
|
+
}
|
1309
|
+
return String(this)
|
1310
|
+
.replace(trimBeginRegexp, "")
|
1311
|
+
.replace(trimEndRegexp, "");
|
1312
|
+
};
|
1313
|
+
}
|
1314
|
+
|
1315
|
+
// ES-5 15.1.2.2
|
1316
|
+
if (parseInt(ws + '08') !== 8 || parseInt(ws + '0x16') !== 22) {
|
1317
|
+
parseInt = (function (origParseInt) {
|
1318
|
+
var hexRegex = /^0[xX]/;
|
1319
|
+
return function parseIntES5(str, radix) {
|
1320
|
+
str = String(str).trim();
|
1321
|
+
if (!+radix) {
|
1322
|
+
radix = hexRegex.test(str) ? 16 : 10;
|
1323
|
+
}
|
1324
|
+
return origParseInt(str, radix);
|
1325
|
+
};
|
1326
|
+
}(parseInt));
|
1327
|
+
}
|
1328
|
+
|
1329
|
+
//
|
1330
|
+
// Util
|
1331
|
+
// ======
|
1332
|
+
//
|
1333
|
+
|
1334
|
+
// ES5 9.4
|
1335
|
+
// http://es5.github.com/#x9.4
|
1336
|
+
// http://jsperf.com/to-integer
|
1337
|
+
|
1338
|
+
function toInteger(n) {
|
1339
|
+
n = +n;
|
1340
|
+
if (n !== n) { // isNaN
|
1341
|
+
n = 0;
|
1342
|
+
} else if (n !== 0 && n !== (1/0) && n !== -(1/0)) {
|
1343
|
+
n = (n > 0 || -1) * Math.floor(Math.abs(n));
|
1344
|
+
}
|
1345
|
+
return n;
|
1346
|
+
}
|
1347
|
+
|
1348
|
+
function isPrimitive(input) {
|
1349
|
+
var type = typeof input;
|
1350
|
+
return (
|
1351
|
+
input === null ||
|
1352
|
+
type === "undefined" ||
|
1353
|
+
type === "boolean" ||
|
1354
|
+
type === "number" ||
|
1355
|
+
type === "string"
|
1356
|
+
);
|
1357
|
+
}
|
1358
|
+
|
1359
|
+
function toPrimitive(input) {
|
1360
|
+
var val, valueOf, toString;
|
1361
|
+
if (isPrimitive(input)) {
|
1362
|
+
return input;
|
1363
|
+
}
|
1364
|
+
valueOf = input.valueOf;
|
1365
|
+
if (typeof valueOf === "function") {
|
1366
|
+
val = valueOf.call(input);
|
1367
|
+
if (isPrimitive(val)) {
|
1368
|
+
return val;
|
1369
|
+
}
|
1370
|
+
}
|
1371
|
+
toString = input.toString;
|
1372
|
+
if (typeof toString === "function") {
|
1373
|
+
val = toString.call(input);
|
1374
|
+
if (isPrimitive(val)) {
|
1375
|
+
return val;
|
1376
|
+
}
|
1377
|
+
}
|
1378
|
+
throw new TypeError();
|
1379
|
+
}
|
1380
|
+
|
1381
|
+
// ES5 9.9
|
1382
|
+
// http://es5.github.com/#x9.9
|
1383
|
+
var toObject = function (o) {
|
1384
|
+
if (o == null) { // this matches both null and undefined
|
1385
|
+
throw new TypeError("can't convert "+o+" to object");
|
1386
|
+
}
|
1387
|
+
return Object(o);
|
1388
|
+
};
|
1389
|
+
|
1390
|
+
}));
|