mirror of
https://github.com/robertkrimen/otto
synced 2025-10-12 20:27:30 +08:00
feat(underscore)!: update to v1.13.6 (#470)
Update underscore.js to v1.13.6. BREAKING CHANGE: This changes the behaviour for the following underscore functions, please see underscore.js documentation for details. * .template * .keys * .after * .range * .reduce * .reduceRight * .reduceLeft Fixes #110
This commit is contained in:
parent
4617108200
commit
8d121dcad1
22
underscore/LICENSE.underscorejs
Normal file
22
underscore/LICENSE.underscorejs
Normal file
|
@ -0,0 +1,22 @@
|
|||
Copyright (c) 2009-2022 Jeremy Ashkenas, Julian Gonggrijp, and DocumentCloud and Investigative Reporters & Editors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation
|
||||
files (the "Software"), to deal in the Software without
|
||||
restriction, including without limitation the rights to use,
|
||||
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -1,11 +0,0 @@
|
|||
.PHONY: source
|
||||
|
||||
source: source.go
|
||||
|
||||
underscore.js:
|
||||
curl -kL http://underscorejs.org/underscore.js > $@
|
||||
|
||||
source.go: underscore.js
|
||||
go-bindata -f underscore -p underscore -u true < $< 2>/dev/null | grep -v '^//' | gofmt > $@
|
||||
head -4 $< >> $@
|
||||
mv $< ..
|
|
@ -1,53 +1,9 @@
|
|||
# underscore
|
||||
--
|
||||
import "github.com/robertkrimen/otto/underscore"
|
||||
|
||||
Package underscore contains the source for the JavaScript utility-belt library.
|
||||
[](https://pkg.go.dev/github.com/robertkrimen/otto/underscore) [](https://opensource.org/licenses/MIT)
|
||||
|
||||
import (
|
||||
_ "github.com/robertkrimen/otto/underscore"
|
||||
)
|
||||
// Every Otto runtime will now include underscore
|
||||
To update the version of underscore run:
|
||||
|
||||
http://underscorejs.org
|
||||
|
||||
https://github.com/documentcloud/underscore
|
||||
|
||||
By importing this package, you'll automatically load underscore every time you
|
||||
create a new Otto runtime.
|
||||
|
||||
To prevent this behavior, you can do the following:
|
||||
|
||||
import (
|
||||
"github.com/robertkrimen/otto/underscore"
|
||||
)
|
||||
|
||||
func init() {
|
||||
underscore.Disable()
|
||||
}
|
||||
|
||||
## Usage
|
||||
|
||||
#### func Disable
|
||||
|
||||
```go
|
||||
func Disable()
|
||||
```shell
|
||||
go generate
|
||||
```
|
||||
Disable underscore runtime inclusion.
|
||||
|
||||
#### func Enable
|
||||
|
||||
```go
|
||||
func Enable()
|
||||
```
|
||||
Enable underscore runtime inclusion.
|
||||
|
||||
#### func Source
|
||||
|
||||
```go
|
||||
func Source() string
|
||||
```
|
||||
Source returns the underscore source.
|
||||
|
||||
--
|
||||
**godocdown** http://github.com/robertkrimen/godocdown
|
||||
|
|
65
underscore/download.go
Normal file
65
underscore/download.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
//go:build generate
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
url = flag.String("url", "", "url to read from")
|
||||
output = flag.String("output", "", "output file to write the result too")
|
||||
)
|
||||
|
||||
func download(url, output string) (err error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("new request failed: %w", err)
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("request failed: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var f *os.File
|
||||
if output != "" {
|
||||
if f, err = os.Create(output); err != nil {
|
||||
return fmt.Errorf("create file %q failed: %w", output, err)
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
} else {
|
||||
f = os.Stdout
|
||||
}
|
||||
|
||||
if _, err := io.Copy(f, resp.Body); err != nil {
|
||||
return fmt.Errorf("body save: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
switch {
|
||||
case len(*url) == 0:
|
||||
log.Fatal("missing required --url parameter")
|
||||
}
|
||||
|
||||
if err := download(*url, *output); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
4
underscore/generate.go
Normal file
4
underscore/generate.go
Normal file
|
@ -0,0 +1,4 @@
|
|||
package underscore
|
||||
|
||||
//go:generate go run download.go --url https://underscorejs.org/underscore-min.js --output underscore-min.js
|
||||
//go:generate go run download.go --url https://raw.githubusercontent.com/jashkenas/underscore/master/LICENSE --output LICENSE.underscorejs
|
3463
underscore/source.go
3463
underscore/source.go
File diff suppressed because it is too large
Load Diff
6
underscore/underscore-min.js
vendored
Normal file
6
underscore/underscore-min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
|
@ -1,33 +1,35 @@
|
|||
/*
|
||||
Package underscore contains the source for the JavaScript utility-belt library.
|
||||
|
||||
import (
|
||||
_ "github.com/robertkrimen/otto/underscore"
|
||||
)
|
||||
// Every Otto runtime will now include underscore
|
||||
|
||||
http://underscorejs.org
|
||||
|
||||
https://github.com/documentcloud/underscore
|
||||
|
||||
By importing this package, you'll automatically load underscore every time you create a new Otto runtime.
|
||||
|
||||
To prevent this behavior, you can do the following:
|
||||
|
||||
import (
|
||||
"github.com/robertkrimen/otto/underscore"
|
||||
)
|
||||
|
||||
func init() {
|
||||
underscore.Disable()
|
||||
}
|
||||
*/
|
||||
// Package underscore contains the source for the JavaScript utility-belt library.
|
||||
//
|
||||
// import (
|
||||
// _ "github.com/robertkrimen/otto/underscore"
|
||||
// )
|
||||
//
|
||||
// Every Otto runtime will now include [underscore] for more information see the [underscore docs]
|
||||
//
|
||||
// By importing this package, you'll automatically load underscore every time you create a new Otto runtime.
|
||||
//
|
||||
// To prevent this behavior, you can do the following:
|
||||
//
|
||||
// import (
|
||||
// "github.com/robertkrimen/otto/underscore"
|
||||
// )
|
||||
//
|
||||
// func init() {
|
||||
// underscore.Disable()
|
||||
// }
|
||||
//
|
||||
// [underscore]: http://underscorejs.org
|
||||
// [underscore docs]: https://github.com/documentcloud/underscore
|
||||
package underscore
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
|
||||
"github.com/robertkrimen/otto/registry"
|
||||
)
|
||||
|
||||
//go:embed underscore-min.js
|
||||
var underscore string
|
||||
var entry *registry.Entry = registry.Register(Source)
|
||||
|
||||
// Enable underscore runtime inclusion.
|
||||
|
@ -42,5 +44,5 @@ func Disable() {
|
|||
|
||||
// Source returns the underscore source.
|
||||
func Source() string {
|
||||
return string(underscore())
|
||||
return underscore
|
||||
}
|
||||
|
|
|
@ -333,7 +333,7 @@ func Test_underscore_arrays_15(t *testing.T) {
|
|||
equal(_.range(0).join(''), '', 'range with 0 as a first argument generates an empty array');
|
||||
equal(_.range(4).join(' '), '0 1 2 3', 'range with a single positive argument generates an array of elements 0,1,2,...,n-1');
|
||||
equal(_.range(5, 8).join(' '), '5 6 7', 'range with two arguments a & b, a<b generates an array of elements a,a+1,a+2,...,b-2,b-1');
|
||||
equal(_.range(8, 5).join(''), '', 'range with two arguments a & b, b<a generates an empty array');
|
||||
equal(_.range(8, 5).join(' '), '8 7 6', 'range with two arguments a & b, b<a generates an array of elements 8,..6');
|
||||
equal(_.range(3, 10, 3).join(' '), '3 6 9', 'range with three arguments a & b & c, c < b-a, a < b generates an array of elements a,a+c,a+2c,...,b - (multiplier of a) < c');
|
||||
equal(_.range(3, 10, 15).join(''), '3', 'range with three arguments a & b & c, c > b-a, a < b generates an array with a single element, equal to a');
|
||||
equal(_.range(12, 7, -2).join(' '), '12 10 8', 'range with three arguments a & b & c, a > b, c < 0 generates an array of elements a,a-c,a-2c and ends with the number not less than b');
|
||||
|
|
|
@ -115,7 +115,7 @@ func Test_underscore_collections_2(t *testing.T) {
|
|||
|
||||
ok(_.reduce(null, function(){}, 138) === 138, 'handles a null (with initial value) properly');
|
||||
equal(_.reduce([], function(){}, undefined), undefined, 'undefined can be passed as a special case');
|
||||
raises(function() { _.reduce([], function(){}); }, TypeError, 'throws an error for empty arrays with no initial value');
|
||||
equal(_.reduce([], function(){}), undefined, 'undefined is the default case');
|
||||
});
|
||||
`)
|
||||
})
|
||||
|
@ -151,7 +151,7 @@ func Test_underscore_collections_3(t *testing.T) {
|
|||
ok(_.reduceRight(null, function(){}, 138) === 138, 'handles a null (with initial value) properly');
|
||||
|
||||
equal(_.reduceRight([], function(){}, undefined), undefined, 'undefined can be passed as a special case');
|
||||
raises(function() { _.reduceRight([], function(){}); }, TypeError, 'throws an error for empty arrays with no initial value');
|
||||
equal(_.reduceRight([], function(){}), undefined, 'undefined for empty array and no initial value');
|
||||
|
||||
// Assert that the correct arguments are being passed.
|
||||
|
||||
|
|
|
@ -82,16 +82,6 @@ func Test_underscore_functions_2(t *testing.T) {
|
|||
curly.sayHi = moe.sayHi;
|
||||
equal(curly.getName(), 'name: curly', 'unbound function is bound to current object');
|
||||
equal(curly.sayHi(), 'hi: moe', 'bound function is still bound to original object');
|
||||
|
||||
curly = {name : 'curly'};
|
||||
moe = {
|
||||
name : 'moe',
|
||||
getName : function() { return 'name: ' + this.name; },
|
||||
sayHi : function() { return 'hi: ' + this.name; }
|
||||
};
|
||||
_.bindAll(moe);
|
||||
curly.sayHi = moe.sayHi;
|
||||
equal(curly.sayHi(), 'hi: moe', 'calling bindAll with no arguments binds all functions to the object');
|
||||
});
|
||||
`)
|
||||
})
|
||||
|
@ -201,7 +191,7 @@ func Test_underscore_functions_7(t *testing.T) {
|
|||
|
||||
equal(testAfter(5, 5), 1, "after(N) should fire after being called N times");
|
||||
equal(testAfter(5, 4), 0, "after(N) should not fire unless called N times");
|
||||
equal(testAfter(0, 0), 1, "after(0) should fire immediately");
|
||||
equal(testAfter(0, 1), 1, "after(0) should fire immediately");
|
||||
});
|
||||
`)
|
||||
})
|
||||
|
|
|
@ -15,11 +15,11 @@ func Test_underscore_objects_0(t *testing.T) {
|
|||
// the test above is not safe because it relies on for-in enumeration order
|
||||
var a = []; a[1] = 0;
|
||||
equal(_.keys(a).join(', '), '1', 'is not fooled by sparse arrays; see issue #95');
|
||||
raises(function() { _.keys(null); }, TypeError, 'throws an error for <null> values');
|
||||
raises(function() { _.keys(void 0); }, TypeError, 'throws an error for <undefined> values');
|
||||
raises(function() { _.keys(1); }, TypeError, 'throws an error for number primitives');
|
||||
raises(function() { _.keys('a'); }, TypeError, 'throws an error for string primitives');
|
||||
raises(function() { _.keys(true); }, TypeError, 'throws an error for boolean primitives');
|
||||
equal(_.keys(null).join(''), '', 'empty array for <null> values');
|
||||
equal(_.keys(void 0).join(''), '', 'empty array for <undefined> values');
|
||||
equal(_.keys(1).join(''), '', 'empty array for number primitives');
|
||||
equal(_.keys('a').join(''), '', 'empty array for string primitives');
|
||||
equal(_.keys(true).join(''), '', 'empty array for boolean primitives');
|
||||
});
|
||||
`)
|
||||
})
|
||||
|
|
|
@ -305,7 +305,7 @@ func Test_underscore_utility_11(t *testing.T) {
|
|||
strictEqual(_.result(obj, 'x'), 'x');
|
||||
strictEqual(_.result(obj, 'y'), 'x');
|
||||
strictEqual(_.result(obj, 'z'), undefined);
|
||||
strictEqual(_.result(null, 'x'), null);
|
||||
strictEqual(_.result(null, 'x'), undefined);
|
||||
});
|
||||
`)
|
||||
})
|
||||
|
@ -320,7 +320,7 @@ func Test_underscore_utility_12(t *testing.T) {
|
|||
test('_.templateSettings.variable', function() {
|
||||
var s = '<%=data.x%>';
|
||||
var data = {x: 'x'};
|
||||
strictEqual(_.template(s, data, {variable: 'data'}), 'x');
|
||||
strictEqual(_.template(s, {variable: 'data'})(data), 'x');
|
||||
_.templateSettings.variable = 'data';
|
||||
strictEqual(_.template(s)(data), 'x');
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user