refactor: combine slug utils into one module

This commit is contained in:
Oliver Eyton-Williams
2019-09-25 20:16:08 +02:00
committed by mrugesh
parent 32ee2f032c
commit 9c2f1ffd82
17 changed files with 33 additions and 81 deletions

View File

@ -10,17 +10,6 @@ export {
renderSignInEmail renderSignInEmail
} from './auth'; } from './auth';
export function dashify(str) {
return ('' + str)
.toLowerCase()
.replace(/\s/g, '-')
.replace(/[^a-z0-9\-.]/gi, '')
.replace(/:/g, '');
}
// todo: unify with server/utils/index.js:dasherize
const dasherize = dashify;
export { dasherize };
export const fixCompletedChallengeItem = obj => export const fixCompletedChallengeItem = obj =>
pick(obj, [ pick(obj, [
'id', 'id',

View File

@ -15,7 +15,7 @@ import isURL from 'validator/lib/isURL';
import { homeLocation } from '../../../config/env'; import { homeLocation } from '../../../config/env';
import { ifNoUserSend } from '../utils/middleware'; import { ifNoUserSend } from '../utils/middleware';
import { dasherize } from '../utils'; import { dasherize } from '../../../utils/slugs';
import _pathMigrations from '../resources/pathMigration.json'; import _pathMigrations from '../resources/pathMigration.json';
import { fixCompletedChallengeItem } from '../../common/utils'; import { fixCompletedChallengeItem } from '../../common/utils';

View File

@ -8,7 +8,7 @@ import { homeLocation } from '../../../config/env';
import nonprofits from '../utils/commit.json'; import nonprofits from '../utils/commit.json';
import { commitGoals, completeCommitment$ } from '../utils/commit'; import { commitGoals, completeCommitment$ } from '../utils/commit';
import { unDasherize } from '../utils'; import { unDasherize } from '../../../utils/slugs';
import { observeQuery, saveInstance } from '../utils/rx'; import { observeQuery, saveInstance } from '../utils/rx';

View File

@ -1,26 +1,3 @@
exports.dasherize = function dasherize(name) {
return ('' + name)
.toLowerCase()
.replace(/\s/g, '-')
.replace(/[^a-z0-9\-.]/gi, '')
.replace(/:/g, '');
};
exports.nameify = function nameify(str) {
return ('' + str).replace(/[^a-zA-Z0-9\s]/g, '').replace(/:/g, '');
};
exports.unDasherize = function unDasherize(name) {
return (
('' + name)
// replace dash with space
.replace(/-/g, ' ')
// strip nonalphanumarics chars except whitespace
.replace(/[^a-zA-Z\d\s]/g, '')
.trim()
);
};
exports.addPlaceholderImage = function addPlaceholderImage(name) { exports.addPlaceholderImage = function addPlaceholderImage(name) {
return `https://example.com/${name}.png`; return `https://example.com/${name}.png`;
}; };

View File

@ -1,7 +1,7 @@
import _ from 'lodash'; import _ from 'lodash';
import { Observable } from 'rx'; import { Observable } from 'rx';
import { unDasherize, nameify } from '../utils'; import { unDasherize, nameify } from '../../../utils/slugs';
import { import {
addNameIdMap as _addNameIdToMap, addNameIdMap as _addNameIdToMap,
checkMapData, checkMapData,

View File

@ -2,7 +2,7 @@ require('dotenv').config();
const { createFilePath } = require('gatsby-source-filesystem'); const { createFilePath } = require('gatsby-source-filesystem');
const { dasherize } = require('./utils'); const { dasherize } = require('../utils/slugs');
const { blockNameify } = require('./utils/blockNameify'); const { blockNameify } = require('./utils/blockNameify');
const { const {
createChallengePages, createChallengePages,

View File

@ -24,7 +24,7 @@ import Hotkeys from '../components/Hotkeys';
import { getGuideUrl } from '../utils'; import { getGuideUrl } from '../utils';
import { challengeTypes } from '../../../../utils/challengeTypes'; import { challengeTypes } from '../../../../utils/challengeTypes';
import { ChallengeNode } from '../../../redux/propTypes'; import { ChallengeNode } from '../../../redux/propTypes';
import { dasherize } from '../../../../utils'; import { dasherize } from '../../../../../utils/slugs';
import { import {
createFiles, createFiles,
challengeFilesSelector, challengeFilesSelector,

View File

@ -8,7 +8,7 @@ import { Button, Modal } from '@freecodecamp/react-bootstrap';
import ga from '../../../analytics'; import ga from '../../../analytics';
import GreenPass from '../../../assets/icons/GreenPass'; import GreenPass from '../../../assets/icons/GreenPass';
import { dasherize } from '../../../../utils'; import { dasherize } from '../../../../../utils/slugs';
import './completion-modal.css'; import './completion-modal.css';

View File

@ -5,13 +5,10 @@ const {
createChallenge, createChallenge,
getChallengesDirForLang getChallengesDirForLang
} = require('../../curriculum/getChallenges'); } = require('../../curriculum/getChallenges');
const utils = require('./'); const { dasherize, nameify } = require('../../utils/slugs');
const { locale } = require('../config/env.json'); const { locale } = require('../config/env.json');
const { blockNameify } = require('./blockNameify'); const { blockNameify } = require('./blockNameify');
const dasherize = utils.dasherize;
const nameify = utils.nameify;
const arrToString = arr => const arrToString = arr =>
Array.isArray(arr) ? arr.join('\n') : _.toString(arr); Array.isArray(arr) ? arr.join('\n') : _.toString(arr);

View File

@ -1,5 +1,5 @@
const path = require('path'); const path = require('path');
const { dasherize } = require('..'); const { dasherize } = require('../../../utils/slugs');
const { viewTypes } = require('../challengeTypes'); const { viewTypes } = require('../challengeTypes');

View File

@ -1,25 +1,3 @@
exports.dasherize = function dasherize(name) {
return ('' + name)
.toLowerCase()
.replace(/\s/g, '-')
.replace(/[^a-z0-9\-.]/gi, '');
};
exports.nameify = function nameify(str) {
return ('' + str).replace(/[^a-zA-Z0-9\s]/g, '').replace(/:/g, '');
};
exports.unDasherize = function unDasherize(name) {
return (
('' + name)
// replace dash with space
.replace(/-/g, ' ')
// strip nonalphanumarics chars except whitespace
.replace(/[^a-zA-Z\d\s]/g, '')
.trim()
);
};
exports.descriptionRegex = /<blockquote|<ol|<h4|<table/; exports.descriptionRegex = /<blockquote|<ol|<h4|<table/;
exports.isBrowser = function isBrowser() { exports.isBrowser = function isBrowser() {

View File

@ -3,7 +3,7 @@ const { findIndex } = require('lodash');
const readDirP = require('readdirp-walk'); const readDirP = require('readdirp-walk');
const { parseMarkdown } = require('@freecodecamp/challenge-md-parser'); const { parseMarkdown } = require('@freecodecamp/challenge-md-parser');
const { dasherize } = require('./utils'); const { dasherize } = require('../utils/slugs');
const challengesDir = path.resolve(__dirname, './challenges'); const challengesDir = path.resolve(__dirname, './challenges');
const metaDir = path.resolve(challengesDir, '_meta'); const metaDir = path.resolve(challengesDir, '_meta');

View File

@ -6,7 +6,7 @@ const adler32 = require('adler32');
const Rx = require('rx'); const Rx = require('rx');
const _ = require('lodash'); const _ = require('lodash');
const createDebugger = require('debug'); const createDebugger = require('debug');
const utils = require('../server/utils'); const { dasherize, nameify } = require('../utils/slugs');
const getChallenges = require('./getChallenges'); const getChallenges = require('./getChallenges');
const { validateChallenge } = require('./schema/challengeSchema'); const { validateChallenge } = require('./schema/challengeSchema');
const app = require('../server/server'); const app = require('../server/server');
@ -16,8 +16,6 @@ const log = createDebugger('fcc:seed');
// this may be brittle // this may be brittle
log.enabled = true; log.enabled = true;
const dasherize = utils.dasherize;
const nameify = utils.nameify;
const Observable = Rx.Observable; const Observable = Rx.Observable;
const Challenge = app.models.Challenge; const Challenge = app.models.Challenge;

View File

@ -1,14 +1,6 @@
const path = require('path'); const path = require('path');
require('dotenv').config({ path: path.resolve(__dirname, '../.env') }); require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
exports.dasherize = function dasherize(name) {
return ('' + name)
.toLowerCase()
.replace(/\s/g, '-')
.replace(/[^a-z0-9\-\.]/gi, '')
.replace(/\:/g, '');
};
const supportedLangs = [ const supportedLangs = [
'arabic', 'arabic',
'chinese', 'chinese',

View File

@ -1,7 +1,7 @@
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const matter = require('gray-matter'); const matter = require('gray-matter');
const { dasherize } = require('../../../client/utils'); const { dasherize } = require('../../../utils/slugs');
const pass = true; const pass = true;

View File

@ -1,6 +1,6 @@
const { flatten } = require('lodash'); const { flatten } = require('lodash');
const { dasherize } = require('../../../api-server/server/utils'); const { dasherize } = require('../../../utils/slugs');
function createPathMigrationMap(curriculum) { function createPathMigrationMap(curriculum) {
return Object.keys(curriculum) return Object.keys(curriculum)

21
utils/slugs.js Normal file
View File

@ -0,0 +1,21 @@
exports.dasherize = function dasherize(name) {
return ('' + name)
.toLowerCase()
.replace(/\s/g, '-')
.replace(/[^a-z0-9\-.]/gi, '');
};
exports.nameify = function nameify(str) {
return ('' + str).replace(/[^a-zA-Z0-9\s]/g, '');
};
exports.unDasherize = function unDasherize(name) {
return (
('' + name)
// replace dash with space
.replace(/-/g, ' ')
// strip nonalphanumarics chars except whitespace
.replace(/[^a-zA-Z\d\s]/g, '')
.trim()
);
};