on master help sign in
lib: amend grants functions for year folders
aeb7c86Matilde Park
lib/lib.js+21−62
@@ -97,76 +97,35 @@ export function formatDateAndTime(dateTimeObject) {
97
97
98// Returns an array of unique grants categories
98// Returns an array of unique grants categories
99export function getGrantsCategories() {
99export function getGrantsCategories() {
100− const paths = fs.readdirSync(directories("grants"));
100+ const years = fs.readdirSync(directories("grants"), { withFileTypes: true }).filter((f) => f.isDirectory());
101− const uniqueCategories = paths
101+ const uniqueCategories = Array.from(new Set(years.map((yearDir) => {
102− // Get only markdown files
102+ return fs.readdirSync(join(directories("grants"), yearDir.name), { withFileTypes: true })
103− .filter((path) => parse(path).ext === ".md")
103+ .filter((path) => parse(path.name).ext === ".md")
104− // Parse frontmatter
105− .map((path) =>
106− matter(fs.readFileSync(join(directories("grants"), path)), options)
104+ .map((path) => matter(fs.readFileSync(join(directories("grants"), yearDir.name, path.name)), options))
107− )
105+ .flat()
108− // Each markdown file has an array of categories. Collapse these arrays into one array.
109− .reduce((acc, metadata) => {
106+ .reduce((acc, metadata) => {
110− return [...acc, ...metadata.data.taxonomies.grant_category];
107+ return [...acc, ...metadata.data.taxonomies.grant_category]
111− }, [])
108+ }, [])
112− // Deduplicate single array of categories such that the output is an array where each category is represented only once.
109+ }).flat()));
113− .reduce((acc, uniqueItem) => {
114− if (acc.includes(uniqueItem)) {
115− return acc;
116− }
117− return [...acc, uniqueItem];
118− }, []);
119 return uniqueCategories;
110 return uniqueCategories;
120}
111}
121
112
122// Returns an array of unique grants types
113// Returns an array of unique grants types
114+
123export function getGrantsTypes() {
115export function getGrantsTypes() {
124− const paths = fs.readdirSync(directories("grants"));
116+ const years = fs.readdirSync(directories("grants"), { withFileTypes: true }).filter((f) => f.isDirectory());
125− const uniqueTypes = paths
117+ const uniqueTypes = Array.from(new Set(years.map((yearDir) => {
126− // Get only markdown files
118+ return fs.readdirSync(join(directories("grants"), yearDir.name), { withFileTypes: true })
127− .filter((path) => parse(path).ext === ".md")
119+ .filter((path) => parse(path.name).ext === ".md")
128− // Parse frontmatter to JSON
129− .map((path) =>
130− matter(fs.readFileSync(join(directories("grants"), path)), options)
120+ .map((path) => matter(fs.readFileSync(join(directories("grants"), yearDir.name, path.name)), options))
131− )
121+ .flat()
132− // Each markdown file has an array of types. Collapse these arrays into one array.
133− .reduce((acc, metadata) => {
122+ .reduce((acc, metadata) => {
134− return [...acc, ...metadata.data.taxonomies.grant_type];
123+ return [...acc, ...metadata.data.taxonomies.grant_type]
135− }, [])
124+ }, [])
136− // Deduplicate single array of types such that the output is an array where each type is represented only once.
125+ }).flat()));
137− .reduce((acc, uniqueItem) => {
138− if (acc.includes(uniqueItem)) {
139− return acc;
140− }
141− return [...acc, uniqueItem];
142− }, []);
143 return uniqueTypes;
126 return uniqueTypes;
144}
127}
145
128
146−// Returns a number which represents how many grants are open, or uncompleted
147−export function getOpenGrantsCount() {
148− const paths = fs.readdirSync(directories("grants"));
149− const count = paths
150− // Get only markdown files
151− .filter((path) => parse(path).ext === ".md")
152− // Parse frontmatter to JSON
153− .map((path) =>
154− matter(fs.readFileSync(join(directories("grants"), path)), options)
155− )
156− // Count grants which are both uncompleted and not assigned
157− .reduce((acc, metadata) => {
158− if (
159− !metadata.data.extra.completed &&
160− !metadata.data.extra.canceled &&
161− metadata.data.extra.assignee?.[0] === ""
162− ) {
163− return acc + 1;
164− }
165− return acc;
166− }, 0);
167− return count;
168−}
169−
170// Groups arrays into an array of arrays with 2 members
129// Groups arrays into an array of arrays with 2 members
171export const pair = (arr) =>
130export const pair = (arr) =>
172 arr.reduce(function (rows, key, index) {
131 arr.reduce(function (rows, key, index) {