mirror of
https://github.com/wassname/side-by-side.git
synced 2026-09-10 12:37:06 +08:00
Fewer parentheses
This commit is contained in:
@@ -8,26 +8,26 @@ angular.module("sideBySide.controllers", [])
|
||||
min = 1
|
||||
max = 5
|
||||
|
||||
scroll = () ->
|
||||
setTimeout () ->
|
||||
if ('section' of route.params)
|
||||
scroll = ->
|
||||
setTimeout ->
|
||||
if 'section' of route.params
|
||||
section = 'section-' + route.params['section']
|
||||
element = document.getElementById(section)
|
||||
element = document.getElementById section
|
||||
$document.scrollTo(element, 10, 200) if element
|
||||
, 1
|
||||
|
||||
$rootScope.$on 'duScrollspy:becameActive', ($event, $element) ->
|
||||
if (not $scope.meta[0].Loading)
|
||||
route.update('section', $element.prop('id').replace('section-', ''))
|
||||
if not $scope.meta[0].Loading
|
||||
route.update 'section', $element.prop('id').replace('section-', '')
|
||||
$rootScope.$apply()
|
||||
|
||||
$scope.$watchCollection () ->
|
||||
$scope.$watchCollection ->
|
||||
poems.getActive()
|
||||
, () ->
|
||||
, ->
|
||||
active = poems.getActive()
|
||||
|
||||
$scope.columns = active.length
|
||||
transformed = transformer(active)
|
||||
transformed = transformer active
|
||||
$scope.verses = transformed.verses
|
||||
$scope.meta = transformed.meta
|
||||
|
||||
@@ -40,9 +40,9 @@ angular.module("sideBySide.controllers", [])
|
||||
$scope.switchActive = (poem) ->
|
||||
length = poems.getActive().length
|
||||
active = poem.meta.Active
|
||||
if ((length > min or not active) and (length < max or active))
|
||||
if (length > min or not active) and (length < max or active)
|
||||
poem.meta.Active = not poem.meta.Active
|
||||
route.update('display', filter.getFilter())
|
||||
route.update 'display', filter.getFilter()
|
||||
# TODO else notification
|
||||
|
||||
$scope.flipPick = () ->
|
||||
@@ -51,7 +51,7 @@ angular.module("sideBySide.controllers", [])
|
||||
$scope.pick = false
|
||||
|
||||
load(
|
||||
route.appUrl + '/' + route.params.base.replace(/\./g, '/')
|
||||
route.appUrl + '/' + route.params.base.replace /\./g, '/'
|
||||
route.params.display
|
||||
)
|
||||
]
|
||||
|
||||
@@ -1,27 +1,24 @@
|
||||
angular.module("sideBySide").service "filter", ['poems', (poems) ->
|
||||
headers = []
|
||||
|
||||
@update = () ->
|
||||
extractHeaders(poems.all)
|
||||
@update = ->
|
||||
extractHeaders poems.all
|
||||
|
||||
@getFilter = () ->
|
||||
@getFilter = ->
|
||||
encodings = []
|
||||
for header in headers
|
||||
activeHeaderValues = _.uniq(poems.getActive().map((item) ->
|
||||
activeHeaderValues = _.uniq poems.getActive().map (item) ->
|
||||
item.meta[header]
|
||||
))
|
||||
inactiveMatching = poems.getInactive().filter((item) ->
|
||||
inactiveMatching = poems.getInactive().filter (item) ->
|
||||
item.meta[header] in activeHeaderValues
|
||||
)
|
||||
if (inactiveMatching.length > 0)
|
||||
if inactiveMatching.length > 0
|
||||
continue # inactive poems have same header value
|
||||
|
||||
encodings.push({ 'header': header, 'values': activeHeaderValues })
|
||||
encodings.push { 'header': header, 'values': activeHeaderValues }
|
||||
|
||||
minlength = _.min(encodings.map((item) -> item.values.length))
|
||||
firstMin = _.first(encodings.filter((item) ->
|
||||
minlength = _.min encodings.map (item) -> item.values.length
|
||||
firstMin = _.first encodings.filter (item) ->
|
||||
item.values.length == minlength
|
||||
))
|
||||
|
||||
ret = {}
|
||||
ret[firstMin.header] = firstMin.values
|
||||
@@ -29,23 +26,19 @@ angular.module("sideBySide").service "filter", ['poems', (poems) ->
|
||||
|
||||
extractHeaders = (all) ->
|
||||
fieldLengths = all
|
||||
.map (poem) ->
|
||||
poem.meta
|
||||
.reduce((fields, meta) ->
|
||||
.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
|
||||
.sortBy ({ 'key': k, 'len': l } for k,l of fieldLengths), 'len'
|
||||
.map (item) -> item.key
|
||||
|
||||
@
|
||||
]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
angular.module("sideBySide").factory "load", ['filter', 'fetch', 'poems', (filter, fetch, poems) ->
|
||||
angular.module("sideBySide").factory "load", [ 'filter', 'fetch', 'poems', (filter, fetch, poems) ->
|
||||
# Load poems
|
||||
#
|
||||
# @param base [String] Path to config file directory
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
angular.module("sideBySide").service "poems", () ->
|
||||
angular.module("sideBySide").service "poems", ->
|
||||
@all = [{
|
||||
meta: {
|
||||
Active: true
|
||||
@@ -14,13 +14,13 @@ angular.module("sideBySide").service "poems", () ->
|
||||
# Get active poems
|
||||
#
|
||||
# @return [Array] Active poems
|
||||
@getActive = () ->
|
||||
@getActive = ->
|
||||
(poem for poem in @all when poem.meta.Active == true)
|
||||
|
||||
# Get inactive poems
|
||||
#
|
||||
# @return [Array] Inactive poems
|
||||
@getInactive = () ->
|
||||
@getInactive = ->
|
||||
(poem for poem in @all when poem.meta.Active != true)
|
||||
|
||||
@
|
||||
|
||||
@@ -7,9 +7,9 @@ angular.module("sideBySide").factory "readerFactory", ['$injector', ($injector)
|
||||
extension = filename.split(".").pop()
|
||||
switch extension
|
||||
when "json"
|
||||
return $injector.get("jsonReader")
|
||||
return $injector.get "jsonReader"
|
||||
when "md", "markdown"
|
||||
return $injector.get("markdownReader")
|
||||
return $injector.get "markdownReader"
|
||||
else
|
||||
throw "Unknown extension '" + extension + "'."
|
||||
]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
angular.module("sideBySide").factory("jsonReader", () ->
|
||||
angular.module("sideBySide").factory("jsonReader", ->
|
||||
# Read json poem
|
||||
#
|
||||
# For sample input, see:
|
||||
@@ -10,7 +10,7 @@ angular.module("sideBySide").factory("jsonReader", () ->
|
||||
parsed = eval('(' + source + ')')
|
||||
|
||||
for i,verse of parsed.content
|
||||
if typeof(verse) == "string"
|
||||
if typeof verse == "string"
|
||||
parsed.content[i] = { section: "", text: verse }
|
||||
else
|
||||
verse.section = "" if "section" not of verse
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
angular.module("sideBySide").factory("markdownReader", () ->
|
||||
angular.module("sideBySide").factory("markdownReader", ->
|
||||
# Read markdown poem
|
||||
#
|
||||
# For sample input, see:
|
||||
@@ -14,9 +14,9 @@ angular.module("sideBySide").factory("markdownReader", () ->
|
||||
# @return [Object] Meta properties
|
||||
readMeta = (text, inlineLexer) ->
|
||||
meta = {}
|
||||
for line in text.split("\n")
|
||||
lexed = inlineLexer.output(line)
|
||||
match = /([^:]*):(.*)/.exec(lexed)
|
||||
for line in text.split "\n"
|
||||
lexed = inlineLexer.output line
|
||||
match = /([^:]*):(.*)/.exec lexed
|
||||
meta[match[1].trim()] = match[2].trim()
|
||||
meta
|
||||
|
||||
@@ -28,7 +28,7 @@ angular.module("sideBySide").factory("markdownReader", () ->
|
||||
items = []
|
||||
while lexed.length > 0
|
||||
break if lexed[0]["type"] in separators
|
||||
items.push(lexed.shift())
|
||||
items.push lexed.shift()
|
||||
items
|
||||
|
||||
# Read content from lexed blocks
|
||||
@@ -46,16 +46,15 @@ angular.module("sideBySide").factory("markdownReader", () ->
|
||||
|
||||
text = readSection(lexed, separators)
|
||||
text.links = lexed.links
|
||||
content.push({
|
||||
content.push {
|
||||
section: heading
|
||||
text: marked.parser(text)
|
||||
})
|
||||
text: marked.parser text
|
||||
}
|
||||
content
|
||||
|
||||
marked.setOptions({ smartypants: true })
|
||||
lexed = marked.lexer(source)
|
||||
meta = lexed.shift()
|
||||
meta = readMeta(meta.text, new marked.InlineLexer(lexed.links))
|
||||
marked.setOptions { smartypants: true }
|
||||
lexed = marked.lexer source
|
||||
meta = readMeta(lexed.shift().text, new marked.InlineLexer lexed.links)
|
||||
separators = if meta.Separator \
|
||||
then [meta.Separator] else ['heading', 'hr']
|
||||
|
||||
|
||||
@@ -3,10 +3,10 @@ angular.module("sideBySide").service "route", ['$location', ($location) ->
|
||||
.split '/'
|
||||
.filter (item) -> item
|
||||
.map (item) -> item.split ':'
|
||||
.reduce((params, param) ->
|
||||
.reduce (params, param) ->
|
||||
key = param.shift()
|
||||
|
||||
if (param.length > 1)
|
||||
if param.length > 1
|
||||
value = {}
|
||||
value[param[0]] = param[1].split ','
|
||||
else
|
||||
@@ -14,7 +14,7 @@ angular.module("sideBySide").service "route", ['$location', ($location) ->
|
||||
|
||||
params[key] = value
|
||||
params
|
||||
, {})
|
||||
, {}
|
||||
|
||||
@params.base = '' if 'base' not of @params
|
||||
|
||||
@@ -22,18 +22,18 @@ angular.module("sideBySide").service "route", ['$location', ($location) ->
|
||||
.substring(0, $location.absUrl().length - $location.url().length)
|
||||
|
||||
getValue = (value) ->
|
||||
if (typeof value == 'object')
|
||||
if typeof value == 'object'
|
||||
for key, val of value
|
||||
# only works for one key...
|
||||
return key + ":" + val.join(',')
|
||||
return key + ":" + val.join ','
|
||||
value
|
||||
|
||||
@get = () ->
|
||||
@get = ->
|
||||
['base', 'display', 'section']
|
||||
.filter (key) => @params[key]
|
||||
.map (key) =>
|
||||
key + ':' + getValue @params[key]
|
||||
.join('/')
|
||||
.join '/'
|
||||
|
||||
@update = (key, value) ->
|
||||
@params[key] = value
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
angular.module("sideBySide").factory("transformer", () ->
|
||||
angular.module("sideBySide").factory("transformer", ->
|
||||
# Transform translations
|
||||
#
|
||||
# @param translations [Array] Translations with 'meta' and 'content' keys
|
||||
@@ -26,8 +26,8 @@ angular.module("sideBySide").factory("transformer", () ->
|
||||
|
||||
details = []
|
||||
while _.size(_.compact(lengths)) > 1
|
||||
mindex = _.indexOf(lengths, _.min(lengths))
|
||||
maxdex = _.indexOf(lengths, _.max(lengths))
|
||||
mindex = _.indexOf(lengths, _.min lengths)
|
||||
maxdex = _.indexOf(lengths, _.max lengths)
|
||||
|
||||
# least occuring count is higher than most occuring count
|
||||
message = if mindex > maxdex \
|
||||
@@ -41,7 +41,7 @@ angular.module("sideBySide").factory("transformer", () ->
|
||||
return "'" + _.last(translation.content).text + "'"
|
||||
)
|
||||
|
||||
details.push(message + lastVerses.join(", "))
|
||||
details.push message + lastVerses.join ", "
|
||||
|
||||
# remove least occuring from lengths
|
||||
delete lengths[mindex]
|
||||
@@ -50,7 +50,7 @@ angular.module("sideBySide").factory("transformer", () ->
|
||||
uneven.join(", ") + "." +
|
||||
" (" + details.join("; ") + ")."
|
||||
|
||||
checkLengths(translations)
|
||||
checkLengths translations
|
||||
|
||||
data = {
|
||||
meta: (translation.meta for translation in translations)
|
||||
|
||||
@@ -1 +1 @@
|
||||
this.getInjector = () -> angular.injector(['ng', 'sideBySide'])
|
||||
this.getInjector = -> angular.injector ['ng', 'sideBySide']
|
||||
|
||||
@@ -2,7 +2,7 @@ filter = {}
|
||||
poemList = {}
|
||||
|
||||
module "filter", {
|
||||
setup: () ->
|
||||
setup: ->
|
||||
poemList.poe = { meta: {
|
||||
Active: false
|
||||
Author: 'Edgar Allan Poe'
|
||||
@@ -47,7 +47,7 @@ module "filter", {
|
||||
filter = injector.get('filter')
|
||||
}
|
||||
|
||||
test "get filter for poems that share a common property", () ->
|
||||
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
|
||||
@@ -57,7 +57,7 @@ test "get filter for poems that share a common property", () ->
|
||||
'Language': [ 'Czech' ]
|
||||
}
|
||||
|
||||
test "get filter for arbitrary poems using shortest key", () ->
|
||||
test "get filter for arbitrary poems using shortest key", ->
|
||||
poemList.vrch.meta.Active = true
|
||||
poemList.muz.meta.Active = true
|
||||
filter.update()
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
poems = {}
|
||||
|
||||
module "poems", {
|
||||
setup: () ->
|
||||
setup: ->
|
||||
poems = getInjector().get 'poems'
|
||||
}
|
||||
|
||||
setPoems = () ->
|
||||
setPoems = ->
|
||||
poems.all = [{
|
||||
meta: { Active: true }
|
||||
content: [{ section: '1', text: 'one' }]
|
||||
@@ -20,18 +20,18 @@ setPoems = () ->
|
||||
content: [{ section: '4', text: 'four' }]
|
||||
}]
|
||||
|
||||
test "has default content", () ->
|
||||
test "has default content", ->
|
||||
active = poems.getActive()
|
||||
equal active[0].content[0].text, 'Please be patient!'
|
||||
equal active[0].content[0].section, 'Loading...'
|
||||
|
||||
test "gets active", () ->
|
||||
test "gets active", ->
|
||||
setPoems()
|
||||
active = poems.getActive()
|
||||
equal active[0].content[0].section, '1'
|
||||
equal active[1].content[0].section, '3'
|
||||
|
||||
test "gets inactive", () ->
|
||||
test "gets inactive", ->
|
||||
setPoems()
|
||||
inactive = poems.getInactive()
|
||||
equal inactive[0].content[0].section, '2'
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
factory = {}
|
||||
|
||||
module "reader factory", {
|
||||
setup: () ->
|
||||
setup: ->
|
||||
factory = getInjector().get 'readerFactory'
|
||||
}
|
||||
|
||||
test "reads markdown", () ->
|
||||
test "reads markdown", ->
|
||||
ok factory "somefile.md"
|
||||
ok factory "otherfile.markdown"
|
||||
|
||||
test "reads json", () ->
|
||||
test "reads json", ->
|
||||
ok factory "fried.json"
|
||||
|
||||
test "doesn't read doc", () ->
|
||||
throws () ->
|
||||
test "doesn't read doc", ->
|
||||
throws ->
|
||||
factory "microsoft.doc"
|
||||
, /unknown/i
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
reader = {}
|
||||
|
||||
module "json reader", {
|
||||
setup: () ->
|
||||
setup: ->
|
||||
reader = getInjector().get 'jsonReader'
|
||||
}
|
||||
|
||||
test "loads poem", () ->
|
||||
test "loads poem", ->
|
||||
deepEqual reader("""
|
||||
{
|
||||
meta: {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
reader = {}
|
||||
|
||||
module "markdown reader", {
|
||||
setup: () ->
|
||||
setup: ->
|
||||
reader = getInjector().get 'markdownReader'
|
||||
}
|
||||
|
||||
test "loads poem with section numbers, paragraphs, and a list", () ->
|
||||
test "loads poem with section numbers, paragraphs, and a list", ->
|
||||
deepEqual reader("""
|
||||
Title: Universal Declaration of Human Rights
|
||||
Author: John Peters Humphrey
|
||||
@@ -57,7 +57,7 @@ test "loads poem with section numbers, paragraphs, and a list", () ->
|
||||
]
|
||||
}
|
||||
|
||||
test "loads poem with leading text and mixed separators", () ->
|
||||
test "loads poem with leading text and mixed separators", ->
|
||||
deepEqual reader("""
|
||||
Title: Test leading text and mixed separators
|
||||
|
||||
@@ -79,7 +79,7 @@ test "loads poem with leading text and mixed separators", () ->
|
||||
]
|
||||
}
|
||||
|
||||
test "loads poem using a custom separator", () ->
|
||||
test "loads poem using a custom separator", ->
|
||||
deepEqual reader("""
|
||||
Title: Separator test
|
||||
Separator: heading
|
||||
|
||||
@@ -7,33 +7,33 @@ setup = (
|
||||
path = '/base:asdf/section:7'
|
||||
hash = '#test'
|
||||
) ->
|
||||
angular.module("sideBySide").service("$location", () ->
|
||||
@path = () -> path
|
||||
@url = () -> path + hash
|
||||
@absUrl = () -> base + path + hash
|
||||
angular.module("sideBySide").service("$location", ->
|
||||
@path = -> path
|
||||
@url = -> path + hash
|
||||
@absUrl = -> base + path + hash
|
||||
@
|
||||
)
|
||||
route = getInjector().get 'route'
|
||||
|
||||
test "has app url", () ->
|
||||
test "has app url", ->
|
||||
setup()
|
||||
equal route.appUrl, 'http://example.com'
|
||||
|
||||
test "gets parameters", () ->
|
||||
test "gets parameters", ->
|
||||
setup()
|
||||
equal route.get(), 'base:asdf/section:7'
|
||||
|
||||
test "updates parameters", () ->
|
||||
test "updates parameters", ->
|
||||
setup()
|
||||
route.update('base', 'zxcv')
|
||||
equal route.get(), 'base:zxcv/section:7'
|
||||
|
||||
test "adds parameter", () ->
|
||||
test "adds parameter", ->
|
||||
setup('http://example.com', '/base:asdf')
|
||||
route.update('section', '1')
|
||||
equal route.get(), 'base:asdf/section:1'
|
||||
|
||||
test "processes arrays from url", () ->
|
||||
test "processes arrays from url", ->
|
||||
setup('http://example.com', '/display:Language:Czech,English', '')
|
||||
|
||||
deepEqual route.params, {
|
||||
@@ -43,7 +43,7 @@ test "processes arrays from url", () ->
|
||||
}
|
||||
}
|
||||
|
||||
test "adds base before section", () ->
|
||||
test "adds base before section", ->
|
||||
setup('http://example.com', '/section:7', '')
|
||||
route.update('base', 'the_raven')
|
||||
equal route.get(), 'base:the_raven/section:7'
|
||||
|
||||
@@ -5,7 +5,7 @@ polish = {}
|
||||
expected = {}
|
||||
|
||||
module "transformer", {
|
||||
setup: () ->
|
||||
setup: ->
|
||||
transformer = getInjector().get 'transformer'
|
||||
|
||||
english = {
|
||||
@@ -58,43 +58,43 @@ module "transformer", {
|
||||
}
|
||||
}
|
||||
|
||||
test "transforms", () ->
|
||||
test "transforms", ->
|
||||
deepEqual transformer([english, czech, polish]), expected
|
||||
|
||||
test "transforms with 2 items", () ->
|
||||
test "transforms with 2 items", ->
|
||||
expected.meta = expected.meta.slice(0, 2)
|
||||
expected.verses = _.map(expected.verses, (translations) ->
|
||||
return translations.slice(0, 2)
|
||||
)
|
||||
deepEqual transformer([english, czech]), expected
|
||||
|
||||
test "throws error on no param", () ->
|
||||
throws () ->
|
||||
test "throws error on no param", ->
|
||||
throws ->
|
||||
transformer()
|
||||
, /no poems found/i
|
||||
|
||||
test "throws error on empty array", () ->
|
||||
throws () ->
|
||||
test "throws error on empty array", ->
|
||||
throws ->
|
||||
transformer []
|
||||
, /no poems found/i
|
||||
|
||||
test "throws error on poem being too short", () ->
|
||||
test "throws error on poem being too short", ->
|
||||
czech.content.pop()
|
||||
throws () ->
|
||||
throws ->
|
||||
transformer [english, czech, polish]
|
||||
, /missing.*nevezmeš jména/i
|
||||
|
||||
test "throws error on poem being too long", () ->
|
||||
test "throws error on poem being too long", ->
|
||||
czech.content.pop()
|
||||
polish.content.pop()
|
||||
throws () ->
|
||||
throws ->
|
||||
transformer [english, czech, polish]
|
||||
, /superfluous.*sabbath/i
|
||||
|
||||
test "throws error on poems too short plus different lengths", () ->
|
||||
test "throws error on poems too short plus different lengths", ->
|
||||
czech.content.pop()
|
||||
polish.content.pop()
|
||||
polish.content.pop()
|
||||
throws () ->
|
||||
throws ->
|
||||
transformer [english, czech, polish]
|
||||
, /missing.*bogów cudzych.*nevezmeš/i
|
||||
|
||||
Reference in New Issue
Block a user