Skip to content
This repository was archived by the owner on Jan 4, 2022. It is now read-only.

Commit 6f8878d

Browse files
committed
clean languages on return
1 parent f14ad93 commit 6f8878d

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@code.gov/api-client",
3-
"version": "0.2.5",
3+
"version": "0.2.6",
44
"description": "Client for Interacting with Code.gov API",
55
"main": "dist/bundle.js",
66
"scripts": {

src/index.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
const { get } = require('axios')
22
const { includes, lower, overlaps, some, trim } = require('@code.gov/cautious')
33

4+
function cleanLanguages(languages) {
5+
const cleaned = []
6+
if (Array.isArray(languages) && languages.length > 0) {
7+
languages.forEach(language => {
8+
if (typeof language === 'string') {
9+
const trimmed = language.trim().toLowerCase()
10+
if (trimmed.length > 0) {
11+
if (!cleaned.includes(trimmed)) {
12+
cleaned.push(trimmed)
13+
}
14+
}
15+
}
16+
})
17+
}
18+
return cleaned
19+
}
20+
21+
function cleanRepo(repo) {
22+
if (repo.languages) {
23+
repo.languages = cleanLanguages(repo.languages)
24+
}
25+
return repo
26+
}
27+
428
class CodeGovAPIClient {
529
constructor(options = {}) {
630
this.base = options.base || 'https://api.code.gov/'
@@ -30,17 +54,10 @@ class CodeGovAPIClient {
3054
}
3155

3256
getJSON(url){
33-
if (this.cache.hasOwnProperty(url)) {
34-
console.log(`[code-gov-api-client] returning data from cache for ${url}`)
35-
return Promise.resolve(this.cache[url])
36-
} else {
37-
this.cache[url] = get(url).then(response => {
38-
const data = response.data
39-
this.cache[url] = data
40-
return data
41-
})
42-
return this.cache[url]
57+
if (!this.cache.hasOwnProperty(url)) {
58+
this.cache[url] = get(url).then(response => response.data)
4359
}
60+
return this.cache[url]
4461
}
4562

4663
/**
@@ -189,7 +206,11 @@ class CodeGovAPIClient {
189206

190207
if (this.debug) console.log('fetching url:', url)
191208

192-
return this.getJSON(url)
209+
return this.getJSON(url).then(dirty => {
210+
console.log('dirty:', dirty);
211+
dirty.repos = dirty.repos.map(cleanRepo)
212+
return dirty
213+
})
193214
}
194215

195216
/**

0 commit comments

Comments
 (0)