sort.ls 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. {filter, flatten, map, sort, sort-by, unique, values} = require 'prelude-ls'
  2. {slugify} = require './slugify.ls'
  3. export slugify-db = (db) ->
  4. list = db
  5. for project in list
  6. project.slug = slugify project.name
  7. project.categories = sort-by (.name.to-lower-case!), project.categories
  8. list = sort-by (.name.to-lower-case!), list
  9. export slugify-list = (list) ->
  10. map (-> { name: it, slug: slugify(it)}), list
  11. export slugify-project = (project) ->
  12. if project.protocols?
  13. project.protocols-slugged = slugify-list project.protocols
  14. if project.categories?
  15. for category in project.categories
  16. category.slug = slugify category.name
  17. category.subcategories = slugify-list category.subcategories
  18. project
  19. export categories-in = (db) ->
  20. list = flatten map (.categories), db
  21. list = map (.name), list
  22. list = unique list
  23. list = slugify-list list
  24. list = sort-by (.name.to-lower-case!), list
  25. export subcategories-in = (category-name, db) ->
  26. list = flatten map (.categories), db
  27. list = filter (.name == category-name), list
  28. list = map (.subcategories), list
  29. list = unique flatten list
  30. list = slugify-list list
  31. list = sort-by (.name.to-lower-case!), list
  32. export subcategories-in-raw = (category-name, db) ->
  33. list = flatten map (.categories), db
  34. list = filter (.name == category-name), list
  35. list = map (.subcategories), list
  36. list = unique flatten list
  37. subcategories-all = (db) ->
  38. tree = categories-in db
  39. subcategories = []
  40. for category in tree
  41. category.subcategories = subcategories-in(category.name, in-this-category(category.name, db))
  42. for subcategory in category.subcategories
  43. subcategories.push subcategory.name
  44. subcategories = sort-by (.to-lower-case!), unique subcategories
  45. export nested-subcategories = (db, rejected) ->
  46. tree = slugify-list subcategories-all db
  47. tree = map ((it) -> name: it.name, slug: it.slug, projects: in-this-subcategory(it.name, db), projects-rejected: in-this-subcategory(it.name, rejected)), tree
  48. tree = unique tree
  49. export in-this-category = (category-name, db) ->
  50. list = []
  51. for project in db
  52. for category in project.categories
  53. if category.name == category-name
  54. list.push project
  55. list = unique list
  56. export in-this-subcategory = (subcategory-name, db) ->
  57. list = []
  58. for project in db
  59. for category in project.categories
  60. for subcategory in category.subcategories
  61. if subcategory == subcategory-name
  62. list.push project
  63. list = unique list
  64. export in-this-protocol = (protocol, db) ->
  65. filter (-> protocol in it.protocols), db
  66. export nested-categories-web = (db) ->
  67. tree = categories-in db
  68. for category in tree
  69. if category.name in <[Routers Servers]>
  70. category.subcategories = subcategories-in(category.name, in-this-category(category.name, db))
  71. category.subcategories = sort-by (.name.to-lower-case!), category.subcategories
  72. for subcategory in category.subcategories
  73. subcategory.projects = in-this-subcategory(subcategory.name, in-this-category(category.name, db))
  74. subcategory.projects = sort-by (.name.to-lower-case!), subcategory.projects
  75. else
  76. cat-subcategories = subcategories-in-raw(category.name, in-this-category(category.name, db))
  77. web-subcategories = subcategories-in-raw('Web Services', in-this-category('Web Services', db))
  78. all-subcategories = slugify-list unique cat-subcategories.concat web-subcategories
  79. category.subcategories = all-subcategories
  80. category.subcategories = sort-by (.name.to-lower-case!), category.subcategories
  81. for subcategory in category.subcategories
  82. cat-projects = in-this-subcategory(subcategory.name, in-this-category(category.name, db))
  83. web-projects = in-this-subcategory(subcategory.name, in-this-category('Web Services', db))
  84. all-projects = sort-by((.name.to-lower-case!), unique(cat-projects.concat(web-projects)))
  85. subcategory.projects = all-projects
  86. tree = sort-by (.name.to-lower-case!), tree
  87. export protocol-types = (protocols) ->
  88. types = categories-in protocols
  89. for type in types
  90. type.protocols = in-this-category(type.name, protocols)
  91. types