walrus-rb 0.10.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/.rspec +1 -0
- data/Gemfile +4 -0
- data/Guardfile +15 -0
- data/MIT-LICENSE +20 -0
- data/README.md +45 -0
- data/Rakefile +17 -0
- data/lib/walrus.rb +7 -0
- data/lib/walrus/config.rb +29 -0
- data/lib/walrus/context.rb +42 -0
- data/lib/walrus/version.rb +3 -0
- data/spec/blocks_spec.rb +13 -0
- data/spec/collections_spec.rb +15 -0
- data/spec/context_spec.rb +13 -0
- data/spec/currencies_spec.rb +13 -0
- data/spec/dates_spec.rb +13 -0
- data/spec/domain_spec.rb +20 -0
- data/spec/filters_spec.rb +13 -0
- data/spec/inflections_spec.rb +13 -0
- data/spec/math_spec.rb +14 -0
- data/spec/spec_helper.rb +34 -0
- data/spec/strings_spec.rb +11 -0
- data/spec/support/javascripts/walrus_domain_objects.js +22 -0
- data/vendor/walrus.collections.js +234 -0
- data/vendor/walrus.currencies.js +57 -0
- data/vendor/walrus.dates.js +296 -0
- data/vendor/walrus.inflections.js +120 -0
- data/vendor/walrus.js +1304 -0
- data/vendor/walrus.math.js +144 -0
- data/vendor/walrus.strings.js +127 -0
- data/walrus.gemspec +30 -0
- metadata +187 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ad98e5ed9edf4b7179704ef6eb50bf2774372c5f
|
4
|
+
data.tar.gz: 563007daa6fe9274d355dba8c170dfabd296a99c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bf7baf99e2572a229b5f8ba94b8bdfe169cddfddf36ca1e2a2b46309b5bd32496ef8b04a93193e33698a562cfcaf79d85a006e36ef24ae37f9f2dcaa5bf3fe3f
|
7
|
+
data.tar.gz: 2f58c35f10cd182c193f2bf8136cc0d0469d22b80207e57eba3ea97a6926da5e49272abf52ca46a26f96c4d6a5696fadb9abcb27631e5362af61026baf119eef
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'spork', :rspec_env => { 'RAILS_ENV' => 'test' } do
|
5
|
+
watch('Gemfile')
|
6
|
+
watch('Guardfile')
|
7
|
+
watch('Gemfile.lock')
|
8
|
+
watch('spec/spec_helper.rb') { :rspec }
|
9
|
+
watch(%r{^spec/support/.+\.rb$})
|
10
|
+
watch(%r{^lib/digital_opera/(.+)\.rb$}) { :rspec }
|
11
|
+
end
|
12
|
+
|
13
|
+
guard :rspec, :all_after_pass => false, :all_on_start => false, :cli => '--drb' do
|
14
|
+
watch(%r{^spec/.+_spec\.rb$})
|
15
|
+
end
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 DIGITALOPERA, LLC
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# walrus-rb
|
2
|
+
|
3
|
+
Uses the actual JavaScript implementation of [walrus.js](https://github.com/jeremyruppel/walrus), but supports using Ruby objects as template contexts and Ruby procs as view functions and domain objects
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'walrus-rb'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install walrus-rb
|
18
|
+
|
19
|
+
## Current Walrus.js Version
|
20
|
+
|
21
|
+
walrus 0.10.1
|
22
|
+
|
23
|
+
## Config
|
24
|
+
|
25
|
+
By default, only the core Walrus library is loaded. To load additional helpers:
|
26
|
+
|
27
|
+
Walrus.configure do |config|
|
28
|
+
config.helper_files = ['collections','currencies','dates','inflections','math','strings']
|
29
|
+
end
|
30
|
+
|
31
|
+
You can also load domain objects
|
32
|
+
|
33
|
+
Walrus.configure do |config|
|
34
|
+
config.domain_objects_file = File.expand_path("../walrus_domain_objects.js", __FILE__)
|
35
|
+
end
|
36
|
+
|
37
|
+
An example domain_objects.js file can be found [here](https://github.com/digitalopera/walrus-rb/blob/master/spec/support/javascripts/walrus_domain_objects.js)
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
1. Fork it ( http://github.com/digitalopera/walrus-rb/fork )
|
42
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
43
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
44
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
45
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'rspec/core'
|
10
|
+
require 'rspec/core/rake_task'
|
11
|
+
|
12
|
+
Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
|
13
|
+
|
14
|
+
desc "Run all specs in spec directory (excluding plugin specs)"
|
15
|
+
RSpec::Core::RakeTask.new
|
16
|
+
task :default => :spec
|
17
|
+
|
data/lib/walrus.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Walrus
|
2
|
+
# Change config options in an initializer:
|
3
|
+
#
|
4
|
+
# Walrus.domain_objects_file = 'domain_objects.js'
|
5
|
+
#
|
6
|
+
# Or in a block:
|
7
|
+
#
|
8
|
+
# Walrus.configure do |config|
|
9
|
+
# config.domain_objects_file = 'domain_objects.js'
|
10
|
+
# end
|
11
|
+
|
12
|
+
module Config
|
13
|
+
extend self
|
14
|
+
|
15
|
+
attr_writer :domain_objects_file, :helper_files
|
16
|
+
|
17
|
+
def configure
|
18
|
+
yield self
|
19
|
+
end
|
20
|
+
|
21
|
+
def domain_objects_file
|
22
|
+
@domain_objects_file ||= ''
|
23
|
+
end
|
24
|
+
|
25
|
+
def helper_files
|
26
|
+
@helper_files ||= []
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'v8'
|
2
|
+
|
3
|
+
module Walrus
|
4
|
+
class Context
|
5
|
+
def initialize
|
6
|
+
@js = V8::Context.new
|
7
|
+
|
8
|
+
# load core file
|
9
|
+
self.load_js File.expand_path("../../../vendor/walrus.js", __FILE__)
|
10
|
+
|
11
|
+
# load additional files
|
12
|
+
Walrus.helper_files.each do |name|
|
13
|
+
self.load_js File.expand_path("../../../vendor/walrus.#{name}.js", __FILE__)
|
14
|
+
end
|
15
|
+
|
16
|
+
# load domain_objects_file is supplied
|
17
|
+
unless Walrus.domain_objects_file.empty?
|
18
|
+
self.load_js Walrus.domain_objects_file
|
19
|
+
end
|
20
|
+
|
21
|
+
return self
|
22
|
+
end
|
23
|
+
|
24
|
+
def parse(*args)
|
25
|
+
@template = walrus.Parser.parse(*args)
|
26
|
+
|
27
|
+
return self
|
28
|
+
end
|
29
|
+
|
30
|
+
def compile(object={})
|
31
|
+
@template.compile(object)
|
32
|
+
end
|
33
|
+
|
34
|
+
def walrus
|
35
|
+
@js.eval('Walrus')
|
36
|
+
end
|
37
|
+
|
38
|
+
def load_js(file_path)
|
39
|
+
@js.load(file_path)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/spec/blocks_spec.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Blocks/Helpers' do
|
4
|
+
let(:template){ Walrus::Context.new.parse(str) }
|
5
|
+
let(:compile){ template.compile({isTrue: true}) }
|
6
|
+
let(:str){ '{{:if @isTrue do}}in if{{end}}' }
|
7
|
+
|
8
|
+
describe ':if' do
|
9
|
+
it 'should render' do
|
10
|
+
compile.should eq 'in if'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'collections' do
|
4
|
+
let(:template){ Walrus::Context.new.parse(str) }
|
5
|
+
let(:compile){ template.compile(numbers) }
|
6
|
+
let(:numbers){ {numbers: [{name:'Bob'},{name: 'Jim'}]} }
|
7
|
+
|
8
|
+
describe ':last' do
|
9
|
+
let(:str){ '{{ :each numbers | :last do }}{{name}}{{end}}' }
|
10
|
+
|
11
|
+
it 'should render' do
|
12
|
+
compile.should eq 'Jim'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Context' do
|
4
|
+
let(:template){ Walrus::Context.new.parse(str) }
|
5
|
+
let(:compile){ template.compile }
|
6
|
+
let(:str){ '{{@name}}' }
|
7
|
+
|
8
|
+
context 'when compile was not passed data' do
|
9
|
+
it 'should not raise error' do
|
10
|
+
expect{ compile }.to_not raise_error
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Currencies' do
|
4
|
+
let(:template){ Walrus::Context.new.parse(str) }
|
5
|
+
let(:compile){ template.compile({isTrue: true}) }
|
6
|
+
let(:str){ '{{ 36000 | :currency }}' }
|
7
|
+
|
8
|
+
describe ':currency' do
|
9
|
+
it 'should render' do
|
10
|
+
compile.should eq '$36,000.00'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/spec/dates_spec.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Dates' do
|
4
|
+
let(:template){ Walrus::Context.new.parse(str) }
|
5
|
+
let(:compile){ template.compile({created_at: '12/12/2001' }) }
|
6
|
+
let(:str){ "{{created_at | :strftime( '%F' )}}" }
|
7
|
+
|
8
|
+
describe ':strftime' do
|
9
|
+
it 'should render' do
|
10
|
+
compile.should eq '2001-12-12'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/spec/domain_spec.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Domain' do
|
4
|
+
let(:context){ Walrus::Context.new }
|
5
|
+
let(:template){ context.parse(str) }
|
6
|
+
let(:compile) do
|
7
|
+
template.compile({
|
8
|
+
employees: [
|
9
|
+
{firstName: 'Jim', lastName: 'Johnson'}
|
10
|
+
]
|
11
|
+
})
|
12
|
+
end
|
13
|
+
let(:str){ '{{:each employees | :as( "person" ) do}}{{fullName()}}{{end}}' }
|
14
|
+
|
15
|
+
it 'should render' do
|
16
|
+
# context.load_js(File.expand_path("../support/javascripts/walrus_domain_objects.js", __FILE__))
|
17
|
+
|
18
|
+
compile.should eq 'Jim Johnson'
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Filters' do
|
4
|
+
let(:template){ Walrus::Context.new.parse(str) }
|
5
|
+
let(:compile){ template.compile({isTrue: true}) }
|
6
|
+
let(:str){ '{{:if @isTrue | :equals(true) do}}in if{{end}}' }
|
7
|
+
|
8
|
+
describe ':equals' do
|
9
|
+
it 'should render' do
|
10
|
+
compile.should eq 'in if'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'inflections' do
|
4
|
+
let(:template){ Walrus::Context.new.parse(str) }
|
5
|
+
let(:compile){ template.compile }
|
6
|
+
let(:str){ "{{ 'book' | :pluralize }}" }
|
7
|
+
|
8
|
+
describe ':strftime' do
|
9
|
+
it 'should render' do
|
10
|
+
compile.should eq 'books'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/spec/math_spec.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'math' do
|
4
|
+
let(:template){ Walrus::Context.new.parse(str) }
|
5
|
+
let(:compile){ template.compile }
|
6
|
+
|
7
|
+
describe ':last' do
|
8
|
+
let(:str){ '{{ 15 | :plus( 6 ) }}' }
|
9
|
+
|
10
|
+
it 'should render' do
|
11
|
+
compile.should eq '21'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
ENV["RAILS_ENV"] ||= 'test'
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'spork'
|
5
|
+
require 'walrus'
|
6
|
+
|
7
|
+
Spork.prefork do
|
8
|
+
require 'rspec/autorun'
|
9
|
+
require 'mocha/api'
|
10
|
+
|
11
|
+
# Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
12
|
+
|
13
|
+
# Configure RSpec ---------------------------------------
|
14
|
+
RSpec.configure do |config|
|
15
|
+
config.mock_framework = :mocha
|
16
|
+
|
17
|
+
# Run specs in random order to surface order dependencies. If you find an
|
18
|
+
# order dependency and want to debug it, you can fix the order by providing
|
19
|
+
# the seed, which is printed after each run.
|
20
|
+
# --seed 1234
|
21
|
+
config.order = "random"
|
22
|
+
# config.order = 63206
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
Walrus.configure do |config|
|
27
|
+
config.domain_objects_file = File.expand_path("../support/javascripts/walrus_domain_objects.js", __FILE__)
|
28
|
+
config.helper_files = ['collections','currencies','dates','inflections','math','strings']
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
Spork.each_run do
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
// Walrus.Domain.person = {
|
2
|
+
// fullName: function(){
|
3
|
+
// return this.firstName + ' ' + this.lastName;
|
4
|
+
// }
|
5
|
+
// }
|
6
|
+
|
7
|
+
(function(){
|
8
|
+
objects = {
|
9
|
+
person: {
|
10
|
+
fullName: function(){
|
11
|
+
return this.firstName + ' ' + this.lastName;
|
12
|
+
}
|
13
|
+
}
|
14
|
+
};
|
15
|
+
|
16
|
+
for (key in objects){
|
17
|
+
if (objects.hasOwnProperty(key)) {
|
18
|
+
Walrus.Domain[key] = objects[key];
|
19
|
+
}
|
20
|
+
}
|
21
|
+
|
22
|
+
})();
|
@@ -0,0 +1,234 @@
|
|
1
|
+
(function() {
|
2
|
+
var Walrus;
|
3
|
+
|
4
|
+
Walrus = (typeof exports !== "undefined" && exports !== null ? require('./walrus') : this).Walrus;
|
5
|
+
|
6
|
+
/**
|
7
|
+
* *:first*
|
8
|
+
* Selects the first _count_ items of the array. Defaults to only the first item.
|
9
|
+
*
|
10
|
+
* Parameters:
|
11
|
+
* count - Optional: how many items to include
|
12
|
+
*
|
13
|
+
* Usage:
|
14
|
+
*
|
15
|
+
* {{ :each numbers | :first do}}
|
16
|
+
* // 1
|
17
|
+
* {{ end }}
|
18
|
+
*
|
19
|
+
* {{ :each numbers | :first( 5 ) do }}
|
20
|
+
* // 1 2 3 4 5
|
21
|
+
* {{ end }}
|
22
|
+
*/
|
23
|
+
|
24
|
+
Walrus.addFilter('first', function(array, count) {
|
25
|
+
if (count != null) {
|
26
|
+
return array.slice(0, count);
|
27
|
+
} else {
|
28
|
+
return array[0];
|
29
|
+
}
|
30
|
+
});
|
31
|
+
|
32
|
+
/**
|
33
|
+
* *:last*
|
34
|
+
* Selects the last _count_ items of the array. Defaults to only the last item.
|
35
|
+
*
|
36
|
+
* Parameters:
|
37
|
+
* count - Optional: how many items to include
|
38
|
+
*
|
39
|
+
* Usage:
|
40
|
+
*
|
41
|
+
* {{ :each numbers | :last do}}
|
42
|
+
* // 10
|
43
|
+
* {{ end }}
|
44
|
+
*
|
45
|
+
* {{ :each numbers | :last( 5 ) do }}
|
46
|
+
* // 6 7 8 9 10
|
47
|
+
* {{ end }}
|
48
|
+
*/
|
49
|
+
|
50
|
+
Walrus.addFilter('last', function(array, count) {
|
51
|
+
if (count != null) {
|
52
|
+
return array.slice(-count);
|
53
|
+
} else {
|
54
|
+
return array[array.length - 1];
|
55
|
+
}
|
56
|
+
});
|
57
|
+
|
58
|
+
/**
|
59
|
+
* *:after*
|
60
|
+
* Selects all of the items in the array except for the first _count_.
|
61
|
+
*
|
62
|
+
* Parameters:
|
63
|
+
* count - how many items to omit from the beginning
|
64
|
+
*
|
65
|
+
* Usage:
|
66
|
+
*
|
67
|
+
* {{ :each numbers | :after( 3 ) do }}
|
68
|
+
* // 4 5 6 7 8 9 10
|
69
|
+
* {{ end }}
|
70
|
+
*/
|
71
|
+
|
72
|
+
Walrus.addFilter('after', function(array, count) {
|
73
|
+
return array.slice(count);
|
74
|
+
});
|
75
|
+
|
76
|
+
/**
|
77
|
+
* *:before*
|
78
|
+
* Selects all of the items in the array except for the last _count_.
|
79
|
+
*
|
80
|
+
* Parameters:
|
81
|
+
* count - how many items to omit from the end
|
82
|
+
*
|
83
|
+
* Usage:
|
84
|
+
*
|
85
|
+
* {{ :each numbers | :before( 3 ) do }}
|
86
|
+
* // 1 2 3 4 5 6 7
|
87
|
+
* {{ end }}
|
88
|
+
*/
|
89
|
+
|
90
|
+
Walrus.addFilter('before', function(array, count) {
|
91
|
+
return array.slice(0, -count);
|
92
|
+
});
|
93
|
+
|
94
|
+
/**
|
95
|
+
* *:count*
|
96
|
+
* Returns the length of the given array.
|
97
|
+
*
|
98
|
+
* Parameters: none
|
99
|
+
*
|
100
|
+
* Usage:
|
101
|
+
*
|
102
|
+
* var numbers = [ 1, 2, 3, 4, 5 ];
|
103
|
+
*
|
104
|
+
* {{ numbers | :count }} // => 5
|
105
|
+
*/
|
106
|
+
|
107
|
+
Walrus.addFilter('count', function(array) {
|
108
|
+
return array.length;
|
109
|
+
});
|
110
|
+
|
111
|
+
/**
|
112
|
+
* *:any*
|
113
|
+
* Returns true if the array is not empty. Opposite of `:empty`.
|
114
|
+
*
|
115
|
+
* Parameters: none
|
116
|
+
*
|
117
|
+
* Usage:
|
118
|
+
*
|
119
|
+
* var numbers = [ 1, 2, 3, 4, 5 ];
|
120
|
+
*
|
121
|
+
* {{ numbers | :any }} // => true
|
122
|
+
*/
|
123
|
+
|
124
|
+
Walrus.addFilter('any', function(array) {
|
125
|
+
return array.length > 0;
|
126
|
+
});
|
127
|
+
|
128
|
+
/**
|
129
|
+
* *:empty*
|
130
|
+
* Returns true of the array is empty. Opposite of `:any`.
|
131
|
+
*
|
132
|
+
* Parameters: none
|
133
|
+
*
|
134
|
+
* Usage:
|
135
|
+
*
|
136
|
+
* var numbers = [ 1, 2, 3, 4, 5 ];
|
137
|
+
*
|
138
|
+
* {{ numbers | :empty }} // => false
|
139
|
+
*/
|
140
|
+
|
141
|
+
Walrus.addFilter('empty', function(array) {
|
142
|
+
return array.length === 0;
|
143
|
+
});
|
144
|
+
|
145
|
+
/**
|
146
|
+
* *:join*
|
147
|
+
* Joins an array into a string, optionally with a separator
|
148
|
+
*
|
149
|
+
* Parameters:
|
150
|
+
* separator - (Optional) string to join on
|
151
|
+
*
|
152
|
+
* Usage:
|
153
|
+
*
|
154
|
+
* var numbers [ 1, 2, 3 ];
|
155
|
+
*
|
156
|
+
* {{ numbers | :join }} // => "123"
|
157
|
+
* {{ numbers | :join( ', ' ) }} // => "1, 2, 3"
|
158
|
+
*/
|
159
|
+
|
160
|
+
Walrus.addFilter('join', function(array, separator) {
|
161
|
+
if (separator == null) separator = '';
|
162
|
+
return array.join(separator);
|
163
|
+
});
|
164
|
+
|
165
|
+
/**
|
166
|
+
* *:sort*
|
167
|
+
* Sorts the members of an array, optionally by a specific field
|
168
|
+
*
|
169
|
+
* Parameters:
|
170
|
+
* field - (Optional) field to compare by
|
171
|
+
*
|
172
|
+
* Usage:
|
173
|
+
*
|
174
|
+
* var names = [ 'Billy', 'Alex', 'Don' ]
|
175
|
+
*
|
176
|
+
* {{ names | :sort }} // => [ 'Alex', 'Billy', 'Don' ]
|
177
|
+
* {{ names | :sort( 'length' ) }} // [ 'Don', 'Billy', 'Alex' ]
|
178
|
+
*/
|
179
|
+
|
180
|
+
Walrus.addFilter('sort', function(array, field) {
|
181
|
+
if (field != null) {
|
182
|
+
return array.sort(function(one, two) {
|
183
|
+
return one[field] > two[field];
|
184
|
+
});
|
185
|
+
} else {
|
186
|
+
return array.sort();
|
187
|
+
}
|
188
|
+
});
|
189
|
+
|
190
|
+
/**
|
191
|
+
* *:in_groups_of*
|
192
|
+
* Splits the given array into sub-arrays with at most `count` items apiece
|
193
|
+
*
|
194
|
+
* Parameters:
|
195
|
+
* count - the number of items to be placed in each group
|
196
|
+
*
|
197
|
+
* Usage:
|
198
|
+
*
|
199
|
+
* var numbers = [ 1, 2, 3, 4, 5 ];
|
200
|
+
*
|
201
|
+
* {{ :each numbers | :in_groups_of( 3 ) }} // => [ [ 1, 2, 3 ], [ 4, 5 ] ]
|
202
|
+
*/
|
203
|
+
|
204
|
+
Walrus.addFilter('in_groups_of', function(array, count) {
|
205
|
+
return Walrus.Utils.reduce(array, [[]], function(memo, item) {
|
206
|
+
var group;
|
207
|
+
group = memo[memo.length - 1];
|
208
|
+
if (group.length < count) {
|
209
|
+
group.push(item);
|
210
|
+
} else {
|
211
|
+
memo.push([item]);
|
212
|
+
}
|
213
|
+
return memo;
|
214
|
+
});
|
215
|
+
});
|
216
|
+
|
217
|
+
/**
|
218
|
+
* *enumerable* domain methods
|
219
|
+
* TODO document these
|
220
|
+
*/
|
221
|
+
|
222
|
+
Walrus.Domain.enumerable = {
|
223
|
+
first: function() {
|
224
|
+
return this['$index'] === 0;
|
225
|
+
},
|
226
|
+
last: function() {
|
227
|
+
return this['$index'] === this['$length'] - 1;
|
228
|
+
},
|
229
|
+
middle: function() {
|
230
|
+
return !(this.first() || this.last());
|
231
|
+
}
|
232
|
+
};
|
233
|
+
|
234
|
+
}).call(this);
|