forked from ffissore/presentz.org
/
utils.coffee
103 lines (84 loc) · 3.3 KB
/
utils.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
exec_for_each = (callable, elements, callback) ->
return callback(undefined, elements) if elements.length is 0
exec_times = 0
for element in elements
callable element, (err) ->
return callback(err) if err?
exec_times++
return callback(undefined, elements) if exec_times is elements.length
cut_string_at = (str, size) ->
if str.length > size + 3
return "#{str.substring(0, size)}..."
str
remove_unwanted_fields_from = (obj, fields_to_remove) ->
for key, value of obj
delete obj[key] if key in fields_to_remove
ensure_only_wanted_fields_in = (obj, allowed_fields) ->
for key, value of obj
delete obj[key] unless key in allowed_fields
remove_unwanted_map_of_fields_from = (objtype, obj, map_of_fields_to_remove) ->
return unless map_of_fields_to_remove[objtype]?
fields_to_remove = map_of_fields_to_remove[objtype]
remove_unwanted_fields_from obj, fields_to_remove
ensure_only_wanted_map_of_fields_in = (objtype, obj, map_of_allowed_fields) ->
return unless map_of_allowed_fields[objtype]?
allowed_fields = map_of_allowed_fields[objtype]
ensure_only_wanted_fields_in obj, allowed_fields
visit_presentation = (presentation, func, fields_or_maps_of_fields) ->
if func.length is 3
apply_on = (objtype, obj) ->
func.call null, objtype, obj, fields_or_maps_of_fields
else
apply_on = (objtype, obj) ->
func.call null, obj, fields_or_maps_of_fields
apply_on "presentation", presentation
if presentation.comments?
for comment in presentation.comments
apply_on "comment", comment
apply_on "user", comment.user if comment.user?
return unless presentation.chapters?
for chapter in presentation.chapters
apply_on "chapter", chapter
apply_on "video", chapter.video if chapter.video?
if chapter.slides?
for slide in chapter.slides
apply_on "slide", slide
if slide.comments?
for comment in slide.comments
apply_on "comment", comment
apply_on "user", comment.user if comment.user?
return
chars = "0123456789qwertyuiopasdfghjklzxcvbnm"
non_word_chars = /[\W]/g
short_words = /\b\w{2}\b/g
generate_id = (title) ->
id = ""
for idx in [0...10]
c = parseInt(Math.random() * chars.length)
id = id.concat(chars[c])
return id unless title? and title isnt ""
title = title.toLowerCase().replace(non_word_chars, " ").replace(short_words, "").replace(/\s/g, "_")
while title.indexOf("__") isnt -1
title = title.replace("__", "_")
title = title.replace(/[_]+$/, "")
id.concat("_", title)
url_regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
is_url_valid = (url) ->
url_regexp.test(url)
type_of = (value) ->
type = toString.call(value).substr(8)
type.substr(0, type.length - 1).toLowerCase()
if exports?
root = exports
else
root = (@utils = {})
root.exec_for_each = exec_for_each
root.cut_string_at = cut_string_at
root.remove_unwanted_fields_from = remove_unwanted_fields_from
root.remove_unwanted_map_of_fields_from = remove_unwanted_map_of_fields_from
root.ensure_only_wanted_fields_in = ensure_only_wanted_fields_in
root.ensure_only_wanted_map_of_fields_in = ensure_only_wanted_map_of_fields_in
root.visit_presentation = visit_presentation
root.generate_id = generate_id
root.is_url_valid = is_url_valid
root.type_of = type_of