Add basic filter

This commit is contained in:
Vit Brunner
2014-12-20 22:10:22 +01:00
parent 26032b4410
commit 734cf5559c
3 changed files with 113 additions and 0 deletions
+2
View File
@@ -49,6 +49,7 @@ module.exports = (grunt) ->
'scripts/services/reader/markdown.js'
'scripts/services/reader/json.js'
'scripts/services/transformer.js'
'scripts/services/filter.js'
'scripts/controllers.js'
]
styles: [
@@ -65,6 +66,7 @@ module.exports = (grunt) ->
'tests/services/reader/markdown.js'
'tests/services/reader/json.js'
'tests/services/transformer.js'
'tests/services/filter.js'
]
test_styles: [
'bower_components/qunit/qunit/qunit.css'
+46
View File
@@ -0,0 +1,46 @@
angular.module("sideBySide").service "filter", ['poems', (poems) ->
headers = []
@update = () ->
extractHeaders(poems.all)
@getFilter = () ->
for header in headers
activeHeaderValues = _.uniq(poems.getActive().map((item) ->
item.meta[header]
))
if (activeHeaderValues.length > 1)
continue # header value differs
inactiveMatching = poems.getInactive().filter((item) ->
item.meta[header] == activeHeaderValues[0]
)
if (inactiveMatching.length > 0)
continue # inactive poems have same header value
ret = {}
ret[header] = activeHeaderValues
return ret
extractHeaders = (all) ->
fieldLengths = all
.map (poem) ->
poem.meta
.reduce((fields, meta) ->
for key, val of meta
if typeof val == 'string'
if key not of fields
fields[key] = 0
fields[key] += val.length
fields
, {})
headers = _
.sortBy((
{ 'key': k, 'len': l } for k,l of fieldLengths
), 'len')
.map (item) ->
item.key
@
]
+65
View File
@@ -0,0 +1,65 @@
filter = {}
poems = {}
poemList = {}
module "filter", {
setup: () ->
poemList.poe = { meta: {
Active: false
Author: 'Edgar Allan Poe'
Title: 'The Raven'
Year: '1845'
Language: 'English'
Code: 'poe'
}}
poemList.vrch = { meta: {
Active: false
Author: 'Jaroslav Vrchlický'
Title: 'Havran'
Year: '1845'
Language: 'Czech'
Code: 'vrch'
}}
poemList.muz = { meta: {
Active: false
Author: 'Augustin Eugen Mužík'
Title: 'Havran'
Year: '1885'
Language: 'Czech'
Code: 'muz'
}}
poemList.dolu = { meta: {
Active: false
Author: 'Karel Dostál Lutinov'
Title: 'Havran'
Year: '1918'
Language: 'Czech'
Code: 'dolu'
}}
angular.module('sideBySide').service('poems', () ->
@all = [
poemList.poe
poemList.vrch
poemList.muz
poemList.dolu
]
@getActive = () ->
(poem for poem in @all when poem.meta.Active is true)
@getInactive = () ->
(poem for poem in @all when poem.meta.Active is not true)
@
)
filter = angular.injector(['ng', 'sideBySide']).get('filter')
}
test "get filter for poems that share a common property", () ->
poemList.dolu.meta.Active = true
poemList.vrch.meta.Active = true
poemList.muz.meta.Active = true
filter.update()
deepEqual filter.getFilter(), {
'Language': [ 'Czech' ]
}