From be6517c02d8c2c6cc9fc61df36fe6543a3bdae82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20No=C3=ABl?= Date: Fri, 20 Mar 2020 19:00:48 +0100 Subject: [PATCH] refacto strapi-generate-* + move nameToSlug to strapi-utils MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pierre Noël --- packages/strapi-generate-api/lib/before.js | 3 +- packages/strapi-generate-api/package.json | 3 +- .../strapi-generate-controller/lib/before.js | 11 ++--- .../strapi-generate-controller/package.json | 3 +- .../templates/controller.template | 2 +- packages/strapi-generate-model/lib/before.js | 13 ++---- packages/strapi-generate-model/package.json | 3 +- .../templates/bookshelf/model.template | 2 +- .../templates/mongoose/model.template | 2 +- .../json/routes.json.js | 18 ++++---- packages/strapi-generate-plugin/lib/before.js | 11 ++--- packages/strapi-generate-plugin/package.json | 3 +- packages/strapi-generate-policy/lib/before.js | 10 ++-- packages/strapi-generate-policy/package.json | 3 +- .../strapi-generate-service/lib/before.js | 11 ++--- packages/strapi-generate-service/package.json | 3 +- .../templates/bookshelf/service.template | 2 +- .../templates/mongoose/service.template | 2 +- .../containers/FormModal/utils/createUid.js | 6 +-- .../src/containers/FormModal/utils/forms.js | 3 +- .../package.json | 1 - .../services/ComponentCategories.js | 2 +- .../services/ContentTypes.js | 2 +- .../schema-builder/component-builder.js | 2 +- .../schema-builder/content-type-builder.js | 46 ++++--------------- .../utils/helpers.js | 7 --- packages/strapi-utils/lib/index.js | 3 ++ packages/strapi-utils/lib/stringFormatting.js | 12 +++++ packages/strapi-utils/package.json | 1 + yarn.lock | 21 +++++++++ 30 files changed, 99 insertions(+), 112 deletions(-) create mode 100644 packages/strapi-utils/lib/stringFormatting.js diff --git a/packages/strapi-generate-api/lib/before.js b/packages/strapi-generate-api/lib/before.js index 48989848b5..28191502e0 100644 --- a/packages/strapi-generate-api/lib/before.js +++ b/packages/strapi-generate-api/lib/before.js @@ -11,6 +11,7 @@ const path = require('path'); // Public node modules. const _ = require('lodash'); const pluralize = require('pluralize'); +const { nameToSlug } = require('strapi-utils'); /** * This `before` function is run before generating targets. @@ -26,7 +27,7 @@ module.exports = (scope, cb) => { } // Format `id`. - const name = scope.name || _.trim(_.camelCase(scope.id)); + const name = scope.name || nameToSlug(scope.id); const environment = process.env.NODE_ENV || 'development'; scope.contentTypeKind = scope.args.kind || 'collectionType'; diff --git a/packages/strapi-generate-api/package.json b/packages/strapi-generate-api/package.json index 10a82fe28d..9b54ce1197 100644 --- a/packages/strapi-generate-api/package.json +++ b/packages/strapi-generate-api/package.json @@ -14,7 +14,8 @@ }, "dependencies": { "lodash": "^4.17.11", - "pluralize": "^7.0.0" + "pluralize": "^7.0.0", + "strapi-utils": "^3.0.0-beta.19.3" }, "scripts": { "test": "echo \"no tests yet\"" diff --git a/packages/strapi-generate-controller/lib/before.js b/packages/strapi-generate-controller/lib/before.js index 82ec1a232f..c206d390c1 100644 --- a/packages/strapi-generate-controller/lib/before.js +++ b/packages/strapi-generate-controller/lib/before.js @@ -6,6 +6,7 @@ // Public node modules. const _ = require('lodash'); +const { nameToSlug } = require('strapi-utils'); /* eslint-disable prefer-template */ /** * This `before` function is run before generating targets. @@ -23,7 +24,7 @@ module.exports = (scope, cb) => { } // Format `id`. - const name = scope.name || _.trim(_.camelCase(scope.id)); + const name = scope.name || nameToSlug(scope.id); // `scope.args` are the raw command line arguments. _.defaults(scope, { @@ -31,12 +32,6 @@ module.exports = (scope, cb) => { api: scope.id, }); - // Determine default values based on the available scope. - _.defaults(scope, { - globalID: _.upperFirst(_.camelCase(scope.id)), - ext: '.js', - }); - // Determine the destination path. let filePath; if (scope.args.api) { @@ -53,7 +48,7 @@ module.exports = (scope, cb) => { _.defaults(scope, { rootPath: scope.rootPath, filePath, - filename: scope.globalID + scope.ext, + filename: `${name}.js`, }); // Trigger callback with no error to proceed. diff --git a/packages/strapi-generate-controller/package.json b/packages/strapi-generate-controller/package.json index 41ac9fdc62..253093716d 100644 --- a/packages/strapi-generate-controller/package.json +++ b/packages/strapi-generate-controller/package.json @@ -14,7 +14,8 @@ "lib": "./lib" }, "dependencies": { - "lodash": "^4.17.11" + "lodash": "^4.17.11", + "strapi-utils": "^3.0.0-beta.19.3" }, "scripts": { "test": "echo \"no tests yet\"" diff --git a/packages/strapi-generate-controller/templates/controller.template b/packages/strapi-generate-controller/templates/controller.template index 12a7559680..929ca9ff11 100644 --- a/packages/strapi-generate-controller/templates/controller.template +++ b/packages/strapi-generate-controller/templates/controller.template @@ -1,7 +1,7 @@ 'use strict'; /** - * A set of functions called "actions" for `<%= globalID %>` + * A set of functions called "actions" for `<%= name %>` */ module.exports = { diff --git a/packages/strapi-generate-model/lib/before.js b/packages/strapi-generate-model/lib/before.js index 55ac5a0983..3aaf69ce34 100644 --- a/packages/strapi-generate-model/lib/before.js +++ b/packages/strapi-generate-model/lib/before.js @@ -11,6 +11,7 @@ const path = require('path'); // Public node modules. const _ = require('lodash'); const pluralize = require('pluralize'); +const { nameToSlug } = require('strapi-utils'); // Fetch stub attribute template on initial load. const attributeTemplate = fs.readFileSync( @@ -35,7 +36,7 @@ module.exports = (scope, cb) => { } // Format `id`. - const name = scope.name || _.trim(_.camelCase(scope.id)); + const name = scope.name || nameToSlug(scope.id); // `scope.args` are the raw command line arguments. _.defaults(scope, { @@ -44,12 +45,6 @@ module.exports = (scope, cb) => { environment: process.env.NODE_ENV || 'development', }); - // Determine default values based on the available scope. - _.defaults(scope, { - globalID: _.upperFirst(name), - ext: '.js', - }); - // Determine the destination path. let filePath; if (scope.args.api) { @@ -64,8 +59,8 @@ module.exports = (scope, cb) => { _.defaults(scope, { rootPath: scope.rootPath, filePath, - filename: scope.globalID + scope.ext, - filenameSettings: scope.globalID + '.settings.json', + filename: `${name}.js`, + filenameSettings: `${name}.settings.json`, }); // Validate optional attribute arguments. diff --git a/packages/strapi-generate-model/package.json b/packages/strapi-generate-model/package.json index 0d455036c6..5d110e15f6 100644 --- a/packages/strapi-generate-model/package.json +++ b/packages/strapi-generate-model/package.json @@ -15,7 +15,8 @@ }, "dependencies": { "lodash": "^4.17.11", - "pluralize": "^7.0.0" + "pluralize": "^7.0.0", + "strapi-utils": "^3.0.0-beta.19.3" }, "scripts": { "test": "echo \"no tests yet\"" diff --git a/packages/strapi-generate-model/templates/bookshelf/model.template b/packages/strapi-generate-model/templates/bookshelf/model.template index f392b92f14..efcdb9e76a 100644 --- a/packages/strapi-generate-model/templates/bookshelf/model.template +++ b/packages/strapi-generate-model/templates/bookshelf/model.template @@ -1,7 +1,7 @@ 'use strict'; /** - * Lifecycle callbacks for the `<%= globalID %>` model. + * Lifecycle callbacks for the `<%= name %>` model. */ module.exports = { diff --git a/packages/strapi-generate-model/templates/mongoose/model.template b/packages/strapi-generate-model/templates/mongoose/model.template index 30172b433f..4416021b74 100644 --- a/packages/strapi-generate-model/templates/mongoose/model.template +++ b/packages/strapi-generate-model/templates/mongoose/model.template @@ -1,7 +1,7 @@ 'use strict'; /** - * Lifecycle callbacks for the `<%= globalID %>` model. + * Lifecycle callbacks for the `<%= name %>` model. */ module.exports = { diff --git a/packages/strapi-generate-plugin/json/routes.json.js b/packages/strapi-generate-plugin/json/routes.json.js index fda8c8c691..5456426c81 100644 --- a/packages/strapi-generate-plugin/json/routes.json.js +++ b/packages/strapi-generate-plugin/json/routes.json.js @@ -7,14 +7,16 @@ module.exports = scope => { function generateRoutes() { return { - routes: [{ - method: 'GET', - path: '/', - handler: scope.globalID + '.index', - config: { - policies: [] - } - }] + routes: [ + { + method: 'GET', + path: '/', + handler: scope.name + '.index', + config: { + policies: [], + }, + }, + ], }; } diff --git a/packages/strapi-generate-plugin/lib/before.js b/packages/strapi-generate-plugin/lib/before.js index aba53cc2f7..2239255a2f 100644 --- a/packages/strapi-generate-plugin/lib/before.js +++ b/packages/strapi-generate-plugin/lib/before.js @@ -8,6 +8,7 @@ const path = require('path'); const fs = require('fs-extra'); const _ = require('lodash'); +const { nameToSlug } = require('strapi-utils'); /** * This `before` function is run before generating targets. @@ -23,13 +24,7 @@ module.exports = (scope, cb) => { } // Format `id`. - const name = scope.name || _.trim(_.camelCase(scope.id)); - - // Determine default values based on the available scope. - _.defaults(scope, { - globalID: _.upperFirst(name), - ext: '.js', - }); + const name = scope.name || nameToSlug(scope.id); // Plugin info. _.defaults(scope, { @@ -42,7 +37,7 @@ module.exports = (scope, cb) => { // Take another pass to take advantage of the defaults absorbed in previous passes. _.defaults(scope, { - filename: `${scope.globalID}${scope.ext}`, + filename: `${name}.js`, filePath: './plugins', }); diff --git a/packages/strapi-generate-plugin/package.json b/packages/strapi-generate-plugin/package.json index f0daa13a0a..046270ebdd 100644 --- a/packages/strapi-generate-plugin/package.json +++ b/packages/strapi-generate-plugin/package.json @@ -14,7 +14,8 @@ }, "dependencies": { "fs-extra": "^8.0.1", - "lodash": "^4.17.11" + "lodash": "^4.17.11", + "strapi-utils": "^3.0.0-beta.19.3" }, "scripts": { "test": "echo \"no tests yet\"" diff --git a/packages/strapi-generate-policy/lib/before.js b/packages/strapi-generate-policy/lib/before.js index 0178dbaebb..03e576a1d7 100644 --- a/packages/strapi-generate-policy/lib/before.js +++ b/packages/strapi-generate-policy/lib/before.js @@ -6,6 +6,7 @@ // Public node modules. const _ = require('lodash'); +const { nameToSlug } = require('strapi-utils'); /** * This `before` function is run before generating targets. @@ -24,7 +25,7 @@ module.exports = (scope, cb) => { } // Format `id`. - const name = scope.name || _.trim(_.camelCase(scope.id)); + const name = scope.name || nameToSlug(scope.id); let filePath; if (scope.args.api) { @@ -35,16 +36,11 @@ module.exports = (scope, cb) => { filePath = './config/policies'; } - // Determine default values based on the available scope. - _.defaults(scope, { - ext: '.js', - }); - // Take another pass to take advantage of the defaults absorbed in previous passes. _.defaults(scope, { name, filePath, - filename: name + scope.ext, + filename: `${name}.js`, }); // Trigger callback with no error to proceed. diff --git a/packages/strapi-generate-policy/package.json b/packages/strapi-generate-policy/package.json index 1b0d408541..b545a4099c 100644 --- a/packages/strapi-generate-policy/package.json +++ b/packages/strapi-generate-policy/package.json @@ -14,7 +14,8 @@ "lib": "./lib" }, "dependencies": { - "lodash": "^4.17.11" + "lodash": "^4.17.11", + "strapi-utils": "^3.0.0-beta.19.3" }, "scripts": { "test": "echo \"no tests yet\"" diff --git a/packages/strapi-generate-service/lib/before.js b/packages/strapi-generate-service/lib/before.js index 0304c665e6..2919c6a633 100644 --- a/packages/strapi-generate-service/lib/before.js +++ b/packages/strapi-generate-service/lib/before.js @@ -6,6 +6,7 @@ // Public node modules. const _ = require('lodash'); +const { nameToSlug } = require('strapi-utils'); /** * This `before` function is run before generating targets. @@ -24,7 +25,7 @@ module.exports = (scope, cb) => { } // Format `id`. - const name = scope.name || _.trim(_.camelCase(scope.id)); + const name = scope.name || nameToSlug(scope.id); // `scope.args` are the raw command line arguments. _.defaults(scope, { @@ -32,12 +33,6 @@ module.exports = (scope, cb) => { api: scope.args.api || scope.id, }); - // Determine default values based on the available scope. - _.defaults(scope, { - globalID: _.upperFirst(name), - ext: '.js', - }); - // Determine the destination path. let filePath; if (scope.args.api) { @@ -52,7 +47,7 @@ module.exports = (scope, cb) => { _.defaults(scope, { rootPath: scope.rootPath, filePath, - filename: scope.globalID + scope.ext, + filename: `${name}.js`, }); // Trigger callback with no error to proceed. diff --git a/packages/strapi-generate-service/package.json b/packages/strapi-generate-service/package.json index 6dd5a14959..ea9c30fb65 100644 --- a/packages/strapi-generate-service/package.json +++ b/packages/strapi-generate-service/package.json @@ -14,7 +14,8 @@ "lib": "./lib" }, "dependencies": { - "lodash": "^4.17.11" + "lodash": "^4.17.11", + "strapi-utils": "^3.0.0-beta.19.3" }, "scripts": { "test": "echo \"no tests yet\"" diff --git a/packages/strapi-generate-service/templates/bookshelf/service.template b/packages/strapi-generate-service/templates/bookshelf/service.template index 511a0c9ac7..2b4df98b29 100644 --- a/packages/strapi-generate-service/templates/bookshelf/service.template +++ b/packages/strapi-generate-service/templates/bookshelf/service.template @@ -1,7 +1,7 @@ 'use strict'; /** - * `<%= globalID %>` service. + * `<%= name %>` service. */ module.exports = { diff --git a/packages/strapi-generate-service/templates/mongoose/service.template b/packages/strapi-generate-service/templates/mongoose/service.template index 511a0c9ac7..2b4df98b29 100644 --- a/packages/strapi-generate-service/templates/mongoose/service.template +++ b/packages/strapi-generate-service/templates/mongoose/service.template @@ -1,7 +1,7 @@ 'use strict'; /** - * `<%= globalID %>` service. + * `<%= name %>` service. */ module.exports = { diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/utils/createUid.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/utils/createUid.js index 9ef96ad8b3..b2552899b6 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/utils/createUid.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/utils/createUid.js @@ -1,6 +1,4 @@ -import slugify from '@sindresorhus/slugify'; - -const nameToSlug = name => slugify(name, { separator: '-' }); +import { nameToSlug } from 'strapi-utils'; const createUid = name => { const modelName = nameToSlug(name); @@ -14,4 +12,4 @@ const createComponentUid = (name, category) => { return `${nameToSlug(category)}.${nameToSlug(name)}`; }; -export { createComponentUid, createUid, nameToSlug }; +export { createComponentUid, createUid }; diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/utils/forms.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/utils/forms.js index bdb4339754..016d873a50 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/utils/forms.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/FormModal/utils/forms.js @@ -3,9 +3,10 @@ import * as yup from 'yup'; import { get, isEmpty, toLower, trim, toNumber } from 'lodash'; import { translatedErrors as errorsTrads } from 'strapi-helper-plugin'; import { FormattedMessage } from 'react-intl'; +import { nameToSlug } from 'strapi-utils'; import pluginId from '../../../pluginId'; import getTrad from '../../../utils/getTrad'; -import { createComponentUid, createUid, nameToSlug } from './createUid'; +import { createComponentUid, createUid } from './createUid'; import componentForm from './componentForm'; import fields from './staticFields'; import { NAME_REGEX, ENUM_REGEX, CATEGORY_NAME_REGEX } from './attributesRegexes'; diff --git a/packages/strapi-plugin-content-type-builder/package.json b/packages/strapi-plugin-content-type-builder/package.json index 9af2f7623d..2a80e9563f 100644 --- a/packages/strapi-plugin-content-type-builder/package.json +++ b/packages/strapi-plugin-content-type-builder/package.json @@ -8,7 +8,6 @@ "description": "content-type-builder.plugin.description" }, "dependencies": { - "@sindresorhus/slugify": "0.9.1", "classnames": "^2.2.6", "fs-extra": "^7.0.0", "immutable": "^3.8.2", diff --git a/packages/strapi-plugin-content-type-builder/services/ComponentCategories.js b/packages/strapi-plugin-content-type-builder/services/ComponentCategories.js index 988fa7e1af..5cba16c3cb 100644 --- a/packages/strapi-plugin-content-type-builder/services/ComponentCategories.js +++ b/packages/strapi-plugin-content-type-builder/services/ComponentCategories.js @@ -2,7 +2,7 @@ const { join } = require('path'); -const { nameToSlug } = require('../utils/helpers'); +const { nameToSlug } = require('strapi-utils'); const createBuilder = require('./schema-builder'); /** diff --git a/packages/strapi-plugin-content-type-builder/services/ContentTypes.js b/packages/strapi-plugin-content-type-builder/services/ContentTypes.js index 9197f06234..c15f767447 100644 --- a/packages/strapi-plugin-content-type-builder/services/ContentTypes.js +++ b/packages/strapi-plugin-content-type-builder/services/ContentTypes.js @@ -7,7 +7,7 @@ const generator = require('strapi-generate'); const createBuilder = require('./schema-builder'); const apiHandler = require('./api-handler'); const { formatAttributes, replaceTemporaryUIDs } = require('../utils/attributes'); -const { nameToSlug } = require('../utils/helpers'); +const { nameToSlug } = require('strapi-utils'); /** * Format a contentType info to be used by the front-end diff --git a/packages/strapi-plugin-content-type-builder/services/schema-builder/component-builder.js b/packages/strapi-plugin-content-type-builder/services/schema-builder/component-builder.js index 6710dc27bf..ecf392d294 100644 --- a/packages/strapi-plugin-content-type-builder/services/schema-builder/component-builder.js +++ b/packages/strapi-plugin-content-type-builder/services/schema-builder/component-builder.js @@ -5,7 +5,7 @@ const _ = require('lodash'); const pluralize = require('pluralize'); const { isConfigurable } = require('../../utils/attributes'); -const { nameToSlug, nameToCollectionName } = require('../../utils/helpers'); +const { nameToSlug, nameToCollectionName } = require('strapi-utils'); const createSchemaHandler = require('./schema-handler'); module.exports = function createComponentBuilder() { diff --git a/packages/strapi-plugin-content-type-builder/services/schema-builder/content-type-builder.js b/packages/strapi-plugin-content-type-builder/services/schema-builder/content-type-builder.js index 0d7507325d..8b15cf986b 100644 --- a/packages/strapi-plugin-content-type-builder/services/schema-builder/content-type-builder.js +++ b/packages/strapi-plugin-content-type-builder/services/schema-builder/content-type-builder.js @@ -5,7 +5,7 @@ const _ = require('lodash'); const pluralize = require('pluralize'); const { isRelation, toUID, isConfigurable } = require('../../utils/attributes'); -const { nameToSlug, nameToCollectionName } = require('../../utils/helpers'); +const { nameToSlug, nameToCollectionName } = require('strapi-utils'); const { typeKinds } = require('../../controllers/validation/constants'); const createSchemaHandler = require('./schema-handler'); @@ -69,9 +69,7 @@ module.exports = function createComponentBuilder() { 'default' ); - const defaultCollectionName = `${nameToCollectionName( - pluralize(infos.name) - )}`; + const defaultCollectionName = `${nameToCollectionName(pluralize(infos.name))}`; // support self referencing content type relation Object.keys(infos.attributes).forEach(key => { @@ -125,31 +123,18 @@ module.exports = function createComponentBuilder() { return _.has(oldAttributes, key) && !isConfigurable(oldAttributes[key]); }); - const newKeys = _.difference( - Object.keys(newAttributes), - Object.keys(oldAttributes) - ); + const newKeys = _.difference(Object.keys(newAttributes), Object.keys(oldAttributes)); - const deletedKeys = _.difference( - Object.keys(oldAttributes), - Object.keys(newAttributes) - ); + const deletedKeys = _.difference(Object.keys(oldAttributes), Object.keys(newAttributes)); - const remainingKeys = _.intersection( - Object.keys(oldAttributes), - Object.keys(newAttributes) - ); + const remainingKeys = _.intersection(Object.keys(oldAttributes), Object.keys(newAttributes)); // remove old relations deletedKeys.forEach(key => { const attribute = oldAttributes[key]; // if the old relation has a target attribute. we need to remove it - if ( - isConfigurable(attribute) && - isRelation(attribute) && - _.has(attribute, 'via') - ) { + if (isConfigurable(attribute) && isRelation(attribute) && _.has(attribute, 'via')) { this.unsetRelation(attribute); } }); @@ -172,15 +157,11 @@ module.exports = function createComponentBuilder() { } if (isRelation(oldAttribute) && isRelation(newAttribute)) { - if ( - _.has(oldAttribute, 'via') && - oldAttribute.via !== newAttribute.targetAttribute - ) { + if (_.has(oldAttribute, 'via') && oldAttribute.via !== newAttribute.targetAttribute) { this.unsetRelation(oldAttribute); } - newAttribute.autoPopulate = - newAttribute.autoPopulate || oldAttribute.autoPopulate; + newAttribute.autoPopulate = newAttribute.autoPopulate || oldAttribute.autoPopulate; return this.setRelation({ key, @@ -239,16 +220,9 @@ module.exports = function createComponentBuilder() { * @param {Object} options options * @param {string} options.name component name */ -const createContentTypeUID = ({ name }) => - `application::${nameToSlug(name)}.${nameToSlug(name)}`; +const createContentTypeUID = ({ name }) => `application::${nameToSlug(name)}.${nameToSlug(name)}`; -const generateRelation = ({ - key, - attribute, - plugin, - modelName, - targetAttribute = {}, -}) => { +const generateRelation = ({ key, attribute, plugin, modelName, targetAttribute = {} }) => { const opts = { via: key, plugin, diff --git a/packages/strapi-plugin-content-type-builder/utils/helpers.js b/packages/strapi-plugin-content-type-builder/utils/helpers.js index 7117247a8d..4a336af25c 100644 --- a/packages/strapi-plugin-content-type-builder/utils/helpers.js +++ b/packages/strapi-plugin-content-type-builder/utils/helpers.js @@ -1,7 +1,5 @@ 'use strict'; -const slugify = require('@sindresorhus/slugify'); - const escapeNewlines = (content = '', placeholder = '\n') => { return content.replace(/[\r\n]+/g, placeholder); }; @@ -26,13 +24,8 @@ const deepTrimObject = attribute => { * Converts a name to a slug * @param {string} name a name to convert */ -const nameToSlug = name => slugify(name, { separator: '-' }); - -const nameToCollectionName = name => slugify(name, { separator: '_' }); module.exports = { escapeNewlines, deepTrimObject, - nameToSlug, - nameToCollectionName, }; diff --git a/packages/strapi-utils/lib/index.js b/packages/strapi-utils/lib/index.js index cd820c9f24..57ad61ca6e 100644 --- a/packages/strapi-utils/lib/index.js +++ b/packages/strapi-utils/lib/index.js @@ -15,6 +15,7 @@ const models = require('./models'); const policy = require('./policy'); const templateConfiguration = require('./templateConfiguration'); const { yup, formatYupErrors } = require('./validators'); +const { nameToSlug, nameToCollectionName } = require('./stringFormatting'); module.exports = { yup, @@ -29,4 +30,6 @@ module.exports = { parseMultipartData, sanitizeEntity, parseType, + nameToSlug, + nameToCollectionName, }; diff --git a/packages/strapi-utils/lib/stringFormatting.js b/packages/strapi-utils/lib/stringFormatting.js new file mode 100644 index 0000000000..425398abed --- /dev/null +++ b/packages/strapi-utils/lib/stringFormatting.js @@ -0,0 +1,12 @@ +'use strict'; + +const slugify = require('@sindresorhus/slugify'); + +const nameToSlug = name => slugify(name, { separator: '-' }); + +const nameToCollectionName = name => slugify(name, { separator: '_' }); + +module.exports = { + nameToSlug, + nameToCollectionName, +}; diff --git a/packages/strapi-utils/package.json b/packages/strapi-utils/package.json index 292aa5a806..34bef87b8e 100644 --- a/packages/strapi-utils/package.json +++ b/packages/strapi-utils/package.json @@ -14,6 +14,7 @@ }, "main": "./lib", "dependencies": { + "@sindresorhus/slugify": "^0.11.0", "date-fns": "^2.8.1", "lodash": "4.17.12", "pino": "^4.7.1", diff --git a/yarn.lock b/yarn.lock index aa75f44ac5..f5ebaae26e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2408,6 +2408,22 @@ escape-string-regexp "^1.0.5" lodash.deburr "^4.1.0" +"@sindresorhus/slugify@^0.11.0": + version "0.11.0" + resolved "https://registry.yarnpkg.com/@sindresorhus/slugify/-/slugify-0.11.0.tgz#642acb99adefa4187285fd17de88745afc161de8" + integrity sha512-ECTZT6z1hYDsopRh8GECaQ5L6hoJHVd4uq5hPi8se9GB31tgtZfnlM8G64hZVhJNmtJ9eIK0SuNhtsaPQStXEQ== + dependencies: + "@sindresorhus/transliterate" "^0.1.0" + escape-string-regexp "^2.0.0" + +"@sindresorhus/transliterate@^0.1.0": + version "0.1.0" + resolved "https://registry.yarnpkg.com/@sindresorhus/transliterate/-/transliterate-0.1.0.tgz#c063bfc4102783fb19c91c2f8c1efb3adfb754be" + integrity sha512-bO6v0M0EuJPjm5Ntfow4nk+r3EZQ41n0ahvAmh766FzPqlm6V/2uDc01vZI3gLeI/1lgV2BTMb6QvxOk9z73ng== + dependencies: + escape-string-regexp "^2.0.0" + lodash.deburr "^4.1.0" + "@snyk/cli-interface@1.5.0": version "1.5.0" resolved "https://registry.yarnpkg.com/@snyk/cli-interface/-/cli-interface-1.5.0.tgz#b9dbe6ebfb86e67ffabf29d4e0d28a52670ac456" @@ -6943,6 +6959,11 @@ escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5: resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ= +escape-string-regexp@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-2.0.0.tgz#a30304e99daa32e23b2fd20f51babd07cffca344" + integrity sha512-UpzcLCXolUWcNu5HtVMHYdXJjArjsF9C0aNnquZYY4uW/Vu0miy5YoWvbV345HauVvcAUnpRuhMMcqTcGOY2+w== + escodegen@1.8.x: version "1.8.1" resolved "https://registry.yarnpkg.com/escodegen/-/escodegen-1.8.1.tgz#5a5b53af4693110bebb0867aa3430dd3b70a1018"