feat: Use prettier-eslint to format code
This commit is contained in:
committed by
mrugesh mohapatra
parent
1ba67c4e2b
commit
b13e5fb41a
@ -64,7 +64,7 @@ export function createActiveUsers() {
|
||||
credentials['client_email'],
|
||||
null,
|
||||
credentials['private_key'],
|
||||
[scope],
|
||||
[scope]
|
||||
);
|
||||
const authorize = observeMethod(client, 'authorize');
|
||||
const options = {
|
||||
@ -89,7 +89,5 @@ export function createActiveUsers() {
|
||||
.do(null, err => console.error(err))
|
||||
// always send a number down
|
||||
.catch(() => Observable.of(0))
|
||||
// cache for 2 seconds to prevent hitting our daily request limit
|
||||
::timeCache(2, 'seconds');
|
||||
}
|
||||
|
||||
|
@ -1,18 +1,18 @@
|
||||
const githubRegex = (/github/i);
|
||||
const githubRegex = /github/i;
|
||||
const providerHash = {
|
||||
facebook: ({ id }) => id,
|
||||
github: ({ username }) => username,
|
||||
twitter: ({ username }) => username,
|
||||
linkedin({ _json }) {
|
||||
return _json && _json.publicProfileUrl || null;
|
||||
return (_json && _json.publicProfileUrl) || null;
|
||||
},
|
||||
google: ({ id }) => id
|
||||
};
|
||||
|
||||
export function getUsernameFromProvider(provider, profile) {
|
||||
return typeof providerHash[provider] === 'function' ?
|
||||
providerHash[provider](profile) :
|
||||
null;
|
||||
return typeof providerHash[provider] === 'function'
|
||||
? providerHash[provider](profile)
|
||||
: null;
|
||||
}
|
||||
|
||||
// createProfileAttributes(provider: String, profile: {}) => Object
|
||||
@ -32,13 +32,7 @@ function createProfileAttributesFromGithub(profile) {
|
||||
const {
|
||||
profileUrl: githubProfile,
|
||||
username,
|
||||
_json: {
|
||||
avatar_url: picture,
|
||||
blog: website,
|
||||
location,
|
||||
bio,
|
||||
name
|
||||
} = {}
|
||||
_json: { avatar_url: picture, blog: website, location, bio, name } = {}
|
||||
} = profile;
|
||||
return {
|
||||
name,
|
||||
|
@ -20,36 +20,34 @@ export function completeCommitment$(user) {
|
||||
isInfosecQaCert
|
||||
} = user;
|
||||
|
||||
return Observable.fromNodeCallback(user.pledge, user)()
|
||||
.flatMap(pledge => {
|
||||
if (!pledge) {
|
||||
return Observable.just('No pledge found');
|
||||
}
|
||||
return Observable.fromNodeCallback(user.pledge, user)().flatMap(pledge => {
|
||||
if (!pledge) {
|
||||
return Observable.just('No pledge found');
|
||||
}
|
||||
|
||||
const { goal } = pledge;
|
||||
const { goal } = pledge;
|
||||
|
||||
if (
|
||||
(isFrontEndCert && goal === commitGoals.frontEndCert) ||
|
||||
(isBackEndCert && goal === commitGoals.backEndCert) ||
|
||||
(isFullStackCert && goal === commitGoals.fullStackCert) ||
|
||||
(isRespWebDesignCert && goal === commitGoals.respWebDesignCert) ||
|
||||
(isFrontEndLibsCert && goal === commitGoals.frontEndLibsCert) ||
|
||||
(isJsAlgoDataStructCert && goal === commitGoals.jsAlgoDataStructCert) ||
|
||||
(isDataVisCert && goal === commitGoals.dataVisCert) ||
|
||||
(isApisMicroservicesCert &&
|
||||
goal === commitGoals.apisMicroservicesCert) ||
|
||||
(isInfosecQaCert && goal === commitGoals.infosecQaCert)
|
||||
) {
|
||||
debug('marking goal complete');
|
||||
pledge.isCompleted = true;
|
||||
pledge.dateEnded = new Date();
|
||||
pledge.formerUserId = pledge.userId;
|
||||
pledge.userId = null;
|
||||
return Observable.fromNodeCallback(pledge.save, pledge)();
|
||||
}
|
||||
return Observable.just(dedent`
|
||||
if (
|
||||
(isFrontEndCert && goal === commitGoals.frontEndCert) ||
|
||||
(isBackEndCert && goal === commitGoals.backEndCert) ||
|
||||
(isFullStackCert && goal === commitGoals.fullStackCert) ||
|
||||
(isRespWebDesignCert && goal === commitGoals.respWebDesignCert) ||
|
||||
(isFrontEndLibsCert && goal === commitGoals.frontEndLibsCert) ||
|
||||
(isJsAlgoDataStructCert && goal === commitGoals.jsAlgoDataStructCert) ||
|
||||
(isDataVisCert && goal === commitGoals.dataVisCert) ||
|
||||
(isApisMicroservicesCert && goal === commitGoals.apisMicroservicesCert) ||
|
||||
(isInfosecQaCert && goal === commitGoals.infosecQaCert)
|
||||
) {
|
||||
debug('marking goal complete');
|
||||
pledge.isCompleted = true;
|
||||
pledge.dateEnded = new Date();
|
||||
pledge.formerUserId = pledge.userId;
|
||||
pledge.userId = null;
|
||||
return Observable.fromNodeCallback(pledge.save, pledge)();
|
||||
}
|
||||
return Observable.just(dedent`
|
||||
You have not yet reached your goal of completing the ${goal}
|
||||
Please retry when you have met the requirements.
|
||||
`);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -8,25 +8,20 @@ export function unwrapHandledError(err) {
|
||||
return err[_handledError] || {};
|
||||
}
|
||||
|
||||
export function wrapHandledError(err, {
|
||||
type,
|
||||
message,
|
||||
redirectTo,
|
||||
status = 200
|
||||
}) {
|
||||
export function wrapHandledError(
|
||||
err,
|
||||
{ type, message, redirectTo, status = 200 }
|
||||
) {
|
||||
err[_handledError] = { type, message, redirectTo, status };
|
||||
return err;
|
||||
}
|
||||
|
||||
// for use with express-validator error formatter
|
||||
export const createValidatorErrorFormatter = (type, redirectTo) =>
|
||||
({ msg }) => wrapHandledError(
|
||||
new Error(msg),
|
||||
{
|
||||
type,
|
||||
message: msg,
|
||||
redirectTo,
|
||||
// we default to 400 as these are malformed requests
|
||||
status: 400
|
||||
}
|
||||
);
|
||||
export const createValidatorErrorFormatter = (type, redirectTo) => ({ msg }) =>
|
||||
wrapHandledError(new Error(msg), {
|
||||
type,
|
||||
message: msg,
|
||||
redirectTo,
|
||||
// we default to 400 as these are malformed requests
|
||||
status: 400
|
||||
});
|
||||
|
@ -3,9 +3,16 @@ import moment from 'moment-timezone';
|
||||
// day count between two epochs (inclusive)
|
||||
export function dayCount([head, tail], timezone = 'UTC') {
|
||||
return Math.ceil(
|
||||
moment(moment(head).tz(timezone).endOf('day')).diff(
|
||||
moment(tail).tz(timezone).startOf('day'),
|
||||
moment(
|
||||
moment(head)
|
||||
.tz(timezone)
|
||||
.endOf('day')
|
||||
).diff(
|
||||
moment(tail)
|
||||
.tz(timezone)
|
||||
.startOf('day'),
|
||||
'days',
|
||||
true)
|
||||
true
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@ -1,8 +1,4 @@
|
||||
export default [
|
||||
'auth',
|
||||
'services',
|
||||
'link'
|
||||
].reduce((throughs, route) => {
|
||||
export default ['auth', 'services', 'link'].reduce((throughs, route) => {
|
||||
throughs[route] = true;
|
||||
return throughs;
|
||||
}, {});
|
||||
|
@ -32,64 +32,59 @@ const getFirstChallenge = _.once(_getFirstChallenge);
|
||||
*/
|
||||
export function _cachedMap({ Block, Challenge }) {
|
||||
const challenges = Challenge.find$({
|
||||
order: [ 'order ASC', 'suborder ASC' ],
|
||||
order: ['order ASC', 'suborder ASC'],
|
||||
where: { isPrivate: false }
|
||||
});
|
||||
const challengeMap = challenges
|
||||
.map(
|
||||
challenges => challenges
|
||||
.map(challenge => challenge.toJSON())
|
||||
.reduce((hash, challenge) => {
|
||||
hash[challenge.dashedName] = challenge;
|
||||
return hash;
|
||||
}, {})
|
||||
);
|
||||
const challengeMap = challenges.map(challenges =>
|
||||
challenges
|
||||
.map(challenge => challenge.toJSON())
|
||||
.reduce((hash, challenge) => {
|
||||
hash[challenge.dashedName] = challenge;
|
||||
return hash;
|
||||
}, {})
|
||||
);
|
||||
const blocks = Block.find$({
|
||||
order: [ 'superOrder ASC', 'order ASC' ],
|
||||
order: ['superOrder ASC', 'order ASC'],
|
||||
where: { isPrivate: false }
|
||||
});
|
||||
const blockMap = Observable.combineLatest(
|
||||
blocks.map(
|
||||
blocks => blocks
|
||||
.map(block => block.toJSON())
|
||||
.reduce((hash, block) => {
|
||||
hash[block.dashedName] = block;
|
||||
return hash;
|
||||
}, {})
|
||||
blocks.map(blocks =>
|
||||
blocks.map(block => block.toJSON()).reduce((hash, block) => {
|
||||
hash[block.dashedName] = block;
|
||||
return hash;
|
||||
}, {})
|
||||
),
|
||||
challenges
|
||||
)
|
||||
.map(([ blocksMap, challenges ]) => {
|
||||
return challenges.reduce((blocksMap, challenge) => {
|
||||
if (blocksMap[challenge.block].challenges) {
|
||||
blocksMap[challenge.block].challenges.push(challenge.dashedName);
|
||||
} else {
|
||||
blocksMap[challenge.block] = {
|
||||
...blocksMap[challenge.block],
|
||||
challenges: [ challenge.dashedName ]
|
||||
};
|
||||
}
|
||||
return blocksMap;
|
||||
}, blocksMap);
|
||||
});
|
||||
const superBlockMap = blocks.map(blocks => blocks.reduce((map, block) => {
|
||||
if (
|
||||
map[block.superBlock] &&
|
||||
map[block.superBlock].blocks
|
||||
) {
|
||||
map[block.superBlock].blocks.push(block.dashedName);
|
||||
} else {
|
||||
map[block.superBlock] = {
|
||||
title: _.startCase(block.superBlock),
|
||||
order: block.superOrder,
|
||||
name: nameify(_.startCase(block.superBlock)),
|
||||
dashedName: block.superBlock,
|
||||
blocks: [block.dashedName],
|
||||
message: block.superBlockMessage
|
||||
};
|
||||
}
|
||||
return map;
|
||||
}, {}));
|
||||
).map(([blocksMap, challenges]) => {
|
||||
return challenges.reduce((blocksMap, challenge) => {
|
||||
if (blocksMap[challenge.block].challenges) {
|
||||
blocksMap[challenge.block].challenges.push(challenge.dashedName);
|
||||
} else {
|
||||
blocksMap[challenge.block] = {
|
||||
...blocksMap[challenge.block],
|
||||
challenges: [challenge.dashedName]
|
||||
};
|
||||
}
|
||||
return blocksMap;
|
||||
}, blocksMap);
|
||||
});
|
||||
const superBlockMap = blocks.map(blocks =>
|
||||
blocks.reduce((map, block) => {
|
||||
if (map[block.superBlock] && map[block.superBlock].blocks) {
|
||||
map[block.superBlock].blocks.push(block.dashedName);
|
||||
} else {
|
||||
map[block.superBlock] = {
|
||||
title: _.startCase(block.superBlock),
|
||||
order: block.superOrder,
|
||||
name: nameify(_.startCase(block.superBlock)),
|
||||
dashedName: block.superBlock,
|
||||
blocks: [block.dashedName],
|
||||
message: block.superBlockMessage
|
||||
};
|
||||
}
|
||||
return map;
|
||||
}, {})
|
||||
);
|
||||
const superBlocks = superBlockMap.map(superBlockMap => {
|
||||
return Object.keys(superBlockMap)
|
||||
.map(key => superBlockMap[key])
|
||||
@ -126,30 +121,25 @@ export function getChallengeById(map, id) {
|
||||
return Observable.if(
|
||||
() => !id,
|
||||
map.map(getFirstChallenge),
|
||||
map.map(addNameIdMap)
|
||||
.map(map => {
|
||||
const {
|
||||
entities: { challenge: challengeMap, challengeIdToName }
|
||||
} = map;
|
||||
let finalChallenge;
|
||||
const dashedName = challengeIdToName[id];
|
||||
finalChallenge = challengeMap[dashedName];
|
||||
if (!finalChallenge) {
|
||||
finalChallenge = getFirstChallenge(map);
|
||||
}
|
||||
return finalChallenge;
|
||||
})
|
||||
map.map(addNameIdMap).map(map => {
|
||||
const {
|
||||
entities: { challenge: challengeMap, challengeIdToName }
|
||||
} = map;
|
||||
let finalChallenge;
|
||||
const dashedName = challengeIdToName[id];
|
||||
finalChallenge = challengeMap[dashedName];
|
||||
if (!finalChallenge) {
|
||||
finalChallenge = getFirstChallenge(map);
|
||||
}
|
||||
return finalChallenge;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export function getChallengeInfo(map) {
|
||||
return map.map(addNameIdMap)
|
||||
.map(({
|
||||
entities: {
|
||||
challenge: challengeMap,
|
||||
challengeIdToName
|
||||
}
|
||||
}) => ({
|
||||
return map
|
||||
.map(addNameIdMap)
|
||||
.map(({ entities: { challenge: challengeMap, challengeIdToName } }) => ({
|
||||
challengeMap,
|
||||
challengeIdToName
|
||||
}));
|
||||
@ -168,42 +158,37 @@ function loadComingSoonOrBetaChallenge({
|
||||
|
||||
// this is a hard search
|
||||
// falls back to soft search
|
||||
export function getChallenge(
|
||||
challengeDashedName,
|
||||
blockDashedName,
|
||||
map) {
|
||||
return map
|
||||
.flatMap(({ entities, result: { superBlocks } }) => {
|
||||
const superBlock = entities.superBlock;
|
||||
const block = entities.block[blockDashedName];
|
||||
const challenge = entities.challenge[challengeDashedName];
|
||||
return Observable.if(
|
||||
() => (
|
||||
!blockDashedName ||
|
||||
!block ||
|
||||
!challenge ||
|
||||
!loadComingSoonOrBetaChallenge(challenge)
|
||||
),
|
||||
getChallengeByDashedName(challengeDashedName, map),
|
||||
Observable.just({ block, challenge })
|
||||
)
|
||||
.map(({ challenge, block }) => ({
|
||||
redirect: challenge.block !== blockDashedName ?
|
||||
`/challenges/${block.dashedName}/${challenge.dashedName}` :
|
||||
false,
|
||||
entities: {
|
||||
superBlock,
|
||||
challenge: {
|
||||
[challenge.dashedName]: challenge
|
||||
}
|
||||
},
|
||||
result: {
|
||||
block: block.dashedName,
|
||||
challenge: challenge.dashedName,
|
||||
superBlocks
|
||||
}
|
||||
}));
|
||||
});
|
||||
export function getChallenge(challengeDashedName, blockDashedName, map) {
|
||||
return map.flatMap(({ entities, result: { superBlocks } }) => {
|
||||
const superBlock = entities.superBlock;
|
||||
const block = entities.block[blockDashedName];
|
||||
const challenge = entities.challenge[challengeDashedName];
|
||||
return Observable.if(
|
||||
() =>
|
||||
!blockDashedName ||
|
||||
!block ||
|
||||
!challenge ||
|
||||
!loadComingSoonOrBetaChallenge(challenge),
|
||||
getChallengeByDashedName(challengeDashedName, map),
|
||||
Observable.just({ block, challenge })
|
||||
).map(({ challenge, block }) => ({
|
||||
redirect:
|
||||
challenge.block !== blockDashedName
|
||||
? `/challenges/${block.dashedName}/${challenge.dashedName}`
|
||||
: false,
|
||||
entities: {
|
||||
superBlock,
|
||||
challenge: {
|
||||
[challenge.dashedName]: challenge
|
||||
}
|
||||
},
|
||||
result: {
|
||||
block: block.dashedName,
|
||||
challenge: challenge.dashedName,
|
||||
superBlocks
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
export function getBlockForChallenge(map, challenge) {
|
||||
@ -211,19 +196,21 @@ export function getBlockForChallenge(map, challenge) {
|
||||
}
|
||||
|
||||
export function getChallengeByDashedName(dashedName, map) {
|
||||
const challengeName = unDasherize(dashedName)
|
||||
.replace(challengesRegex, '');
|
||||
const challengeName = unDasherize(dashedName).replace(challengesRegex, '');
|
||||
const testChallengeName = new RegExp(challengeName, 'i');
|
||||
|
||||
return map
|
||||
.map(({ entities }) => entities.challenge)
|
||||
.flatMap(challengeMap => {
|
||||
return Observable.from(Object.keys(challengeMap))
|
||||
.map(key => challengeMap[key]);
|
||||
return Observable.from(Object.keys(challengeMap)).map(
|
||||
key => challengeMap[key]
|
||||
);
|
||||
})
|
||||
.filter(challenge => {
|
||||
return loadComingSoonOrBetaChallenge(challenge) &&
|
||||
testChallengeName.test(challenge.name);
|
||||
return (
|
||||
loadComingSoonOrBetaChallenge(challenge) &&
|
||||
testChallengeName.test(challenge.name)
|
||||
);
|
||||
})
|
||||
.last({ defaultValue: null })
|
||||
.flatMap(challengeOrNull => {
|
||||
@ -234,8 +221,9 @@ export function getChallengeByDashedName(dashedName, map) {
|
||||
);
|
||||
})
|
||||
.flatMap(challenge => {
|
||||
return getBlockForChallenge(map, challenge)
|
||||
.map(block => ({ challenge, block }));
|
||||
return getBlockForChallenge(map, challenge).map(block => ({
|
||||
challenge,
|
||||
block
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -57,9 +57,11 @@ export const userPropsForSession = [
|
||||
export function normaliseUserFields(user) {
|
||||
const about = user.bio && !user.about ? user.bio : user.about;
|
||||
const picture = user.picture || addPlaceholderImage(user.username);
|
||||
const twitter = user.twitter && isURL(user.twitter) ?
|
||||
user.twitter :
|
||||
user.twitter && `https://www.twitter.com/${user.twitter.replace(/^@/, '')}`;
|
||||
const twitter =
|
||||
user.twitter && isURL(user.twitter)
|
||||
? user.twitter
|
||||
: user.twitter &&
|
||||
`https://www.twitter.com/${user.twitter.replace(/^@/, '')}`;
|
||||
return { about, picture, twitter };
|
||||
}
|
||||
|
||||
|
@ -49,7 +49,9 @@ export function timeCache(time, unit) {
|
||||
// set new expire time in MS and create new subscription to source
|
||||
if (!expireCacheAt || expireCacheAt < Date.now()) {
|
||||
// set expire in ms;
|
||||
expireCacheAt = moment().add(time, unit).valueOf();
|
||||
expireCacheAt = moment()
|
||||
.add(time, unit)
|
||||
.valueOf();
|
||||
cache = new AsyncSubject();
|
||||
source.subscribe(cache);
|
||||
}
|
||||
|
@ -25,13 +25,7 @@ export function getHost() {
|
||||
|
||||
export function getServerFullURL() {
|
||||
if (!isDev) {
|
||||
return getProtocol()
|
||||
+ '://'
|
||||
+ getHost();
|
||||
return getProtocol() + '://' + getHost();
|
||||
}
|
||||
return getProtocol()
|
||||
+ '://'
|
||||
+ getHost()
|
||||
+ ':'
|
||||
+ getPort();
|
||||
return getProtocol() + '://' + getHost() + ':' + getPort();
|
||||
}
|
||||
|
@ -13,7 +13,6 @@ const hoursBetween = 24;
|
||||
const hoursDay = 24;
|
||||
|
||||
export function prepUniqueDaysByHours(cals, tz = 'UTC') {
|
||||
|
||||
let prev = null;
|
||||
|
||||
// compose goes bottom to top (map > sortBy > transform)
|
||||
@ -25,27 +24,34 @@ export function prepUniqueDaysByHours(cals, tz = 'UTC') {
|
||||
} else if (
|
||||
moment(cur)
|
||||
.tz(tz)
|
||||
.diff(moment(prev).tz(tz).startOf('day'), 'hours')
|
||||
>= hoursDay
|
||||
.diff(
|
||||
moment(prev)
|
||||
.tz(tz)
|
||||
.startOf('day'),
|
||||
'hours'
|
||||
) >= hoursDay
|
||||
) {
|
||||
data.push(cur);
|
||||
prev = cur;
|
||||
}
|
||||
}, []),
|
||||
sortBy(e => e),
|
||||
map(ts => moment(ts).tz(tz).startOf('hours').valueOf())
|
||||
map(ts =>
|
||||
moment(ts)
|
||||
.tz(tz)
|
||||
.startOf('hours')
|
||||
.valueOf()
|
||||
)
|
||||
)(cals);
|
||||
}
|
||||
|
||||
export function calcCurrentStreak(cals, tz = 'UTC') {
|
||||
|
||||
let prev = last(cals);
|
||||
if (
|
||||
moment()
|
||||
.tz(tz)
|
||||
.startOf('day')
|
||||
.diff(moment(prev).tz(tz), 'hours')
|
||||
> hoursBetween
|
||||
.diff(moment(prev).tz(tz), 'hours') > hoursBetween
|
||||
) {
|
||||
return 0;
|
||||
}
|
||||
@ -56,8 +62,7 @@ export function calcCurrentStreak(cals, tz = 'UTC') {
|
||||
moment(prev)
|
||||
.tz(tz)
|
||||
.startOf('day')
|
||||
.diff(moment(cur).tz(tz), 'hours')
|
||||
<= hoursBetween
|
||||
.diff(moment(cur).tz(tz), 'hours') <= hoursBetween
|
||||
) {
|
||||
prev = cur;
|
||||
currentStreak++;
|
||||
@ -72,20 +77,26 @@ export function calcCurrentStreak(cals, tz = 'UTC') {
|
||||
}
|
||||
|
||||
export function calcLongestStreak(cals, tz = 'UTC') {
|
||||
|
||||
let tail = cals[0];
|
||||
const longest = cals.reduce((longest, head, index) => {
|
||||
const last = cals[index === 0 ? 0 : index - 1];
|
||||
// is streak broken
|
||||
if (moment(head).tz(tz).startOf('day').diff(moment(last).tz(tz), 'hours')
|
||||
> hoursBetween) {
|
||||
tail = head;
|
||||
}
|
||||
if (dayCount(longest, tz) < dayCount([head, tail], tz)) {
|
||||
return [head, tail];
|
||||
}
|
||||
return longest;
|
||||
}, [cals[0], cals[0]]);
|
||||
const longest = cals.reduce(
|
||||
(longest, head, index) => {
|
||||
const last = cals[index === 0 ? 0 : index - 1];
|
||||
// is streak broken
|
||||
if (
|
||||
moment(head)
|
||||
.tz(tz)
|
||||
.startOf('day')
|
||||
.diff(moment(last).tz(tz), 'hours') > hoursBetween
|
||||
) {
|
||||
tail = head;
|
||||
}
|
||||
if (dayCount(longest, tz) < dayCount([head, tail], tz)) {
|
||||
return [head, tail];
|
||||
}
|
||||
return longest;
|
||||
},
|
||||
[cals[0], cals[0]]
|
||||
);
|
||||
|
||||
return dayCount(longest, tz);
|
||||
}
|
||||
|
Reference in New Issue
Block a user