sindresorhus / slugify Goto Github PK
View Code? Open in Web Editor NEWSlugify a string
License: MIT License
Slugify a string
License: MIT License
My use case is create usernames without separator.
Following the documentation: https://github.com/sindresorhus/slugify#separator
I have tried with:
slugify('BAR and baz', {separator: ''});
And I expect this:
'barandbaz'
But I get an error:
SyntaxError: Invalid regular expression: /{2,}/: Nothing to repeat
Slugify seems natively incompatible with jest. I get the following error when I run jest test where slugify is used in one of the test:
Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/home/runner/slug/node_modules/@sindresorhus/slugify/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import escapeStringRegexp from 'escape-string-regexp';
^^^^^^
SyntaxError: Cannot use import statement outside a module
1 | 'use strict'
2 |
> 3 | const slugify = require('@sindresorhus/slugify');
| ^
4 |
5 | describe('slugify', () => {
6 | test('test 1', () => {
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (slug.test.js:3:17)
My files:
// slug.test.js
'use strict'
const slugify = require('@sindresorhus/slugify');
describe('slugify', () => {
test('test 1', () => {
expect(slugify('démocratie')).toBe('democratie');
});
});
// package.json
{
"name": "slug",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@sindresorhus/slugify": "^2.1.0"
},
"dependencies": {
"jest": "^27.4.7"
}
}
Here is a repo that reproduces the error when you run jest
or yarn test
: https://replit.com/@PierreNoel/slug#slug.test.js
We got the following errors in IE11:
slugify('XBOX360');
returns "xbo-x360". But I expected the value to be "xbox360"
Trying to use it with create-react-app, it fails the minification:
Failed to minify the code from this file:
./node_modules/@sindresorhus/slugify/index.js:7
Read more here: http://bit.ly/2tRViJ9
I tried
customReplacements: [
['[', '['],
[']', ']']
]
to force not remove brackets [] but still removed in my slugified string...
Hello!
Thank you for this great library!
However, would you consider to extract the transliteration part into it's own library? I believe this functionality could be very much useful on it's own.
Sometimes leading underscores are intentional (file names designed to represent hidden paths on a website). It would be nice if this could be supported.
A plural of an acronym with decamelization enabled currently results in:
slugify('Util APIs'); // util-api-s
It would be nicer if plurals were detected and ignored during decamelization so that it can produce util-apis
slug instead.
Would be great if you could config compund slugs with 2 or more fields like title and id.
HI Sindre
I have been using your slugify and I find it very useful. So thank you for offering it.
I discovered that a url containing a dot is converted. Eg:
version3.1 is converted to version3-1
This created problems when I'm creating slugs for URLs like https://api.insightly.com/v3.1/Help
According to the spec of url written by Tim Berners-Lee back in 1994 it is legal to have a dot "." (punktum) in a url.
https://www.w3.org/Addressing/URL/url-spec.txt
Is there a way to instruct slugify to not convert "." to "-" ?
First of all thanks for the great package. I am wondering whether it is possible to transpile the package into ES5 so it can be used in older browsers as well. For example, if the package is used in IE11 the arrow function is not supported and therefore results in an error. Edge 18 does not support spread syntax so also results in an error.
Of course the app that uses the package can transpile the package itself but that feels more like an overhead. Compiling the package to ES5 in the first place should not bring any problems on the other hand.
Error [ERR_REQUIRE_ESM]: require() of ES Module
import Slugify from '@sindresorhus/slugify'
node_modules/@sindresorhus/slugify/index.js from .../vite.config.ts not supported.
Am I doing something wrong?
https://github.com/jeremys/uslug
i use this plugin is ok
slugify: (s: string) => uslug(s),
I am well aware of , {decamelize: false}
.
Wanted to report an edge case.
slugify('JAMstack Conf London 2019')
// ja-mstack-conf-london-2019
I think it should do jam-stack-conf-london-2019
in case, where all the caps are considered one single word.
Is it possible to truncate the output of the "slugified" string if it's way too long? Thanks!
Hey! I'm probably too much of a newb here, but I can install all of your node modules so far, except this one ;-)
I'm using node v8.10.0, and npm 5.6.0. Thanks!
$ npm install @sindresorhus/slugify
npm ERR! code E404
npm ERR! 404 Not Found: @sindresorhus/slugify@latest
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/bparks/.npm/cache/_logs/2018-04-27T11_36_52_196Z-debug.log
and that log file has this in it
0 info it worked if it ends with ok
1 verbose cli [ '/Users/bparks/.nvm/versions/node/v8.10.0/bin/node',
1 verbose cli '/Users/bparks/.nvm/versions/node/v8.10.0/bin/npm',
1 verbose cli 'install',
1 verbose cli '@sindresorhus/slugify' ]
2 info using [email protected]
3 info using [email protected]
4 verbose npm-session 1b1c4188ad8951d9
5 silly install loadCurrentTree
6 silly install readLocalPackageData
7 http fetch GET 404 https://nexus.dev.ca3.sfmc.co/nexus/content/repositories/npm-all/@sindresorhus%2fslugify 859ms
8 silly fetchPackageMetaData error for @sindresorhus/slugify@latest 404 Not Found: @sindresorhus/slugify@latest
9 verbose stack Error: 404 Not Found: @sindresorhus/slugify@latest
9 verbose stack at fetch.then.res (/Users/bparks/.nvm/versions/node/v8.10.0/lib/node_modules/npm/node_modules/pacote/lib/fetchers/registry/fetch.js:42:19)
9 verbose stack at tryCatcher (/Users/bparks/.nvm/versions/node/v8.10.0/lib/node_modules/npm/node_modules/bluebird/js/release/util.js:16:23)
9 verbose stack at Promise._settlePromiseFromHandler (/Users/bparks/.nvm/versions/node/v8.10.0/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:512:31)
9 verbose stack at Promise._settlePromise (/Users/bparks/.nvm/versions/node/v8.10.0/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:569:18)
9 verbose stack at Promise._settlePromise0 (/Users/bparks/.nvm/versions/node/v8.10.0/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:614:10)
9 verbose stack at Promise._settlePromises (/Users/bparks/.nvm/versions/node/v8.10.0/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:693:18)
9 verbose stack at Async._drainQueue (/Users/bparks/.nvm/versions/node/v8.10.0/lib/node_modules/npm/node_modules/bluebird/js/release/async.js:133:16)
9 verbose stack at Async._drainQueues (/Users/bparks/.nvm/versions/node/v8.10.0/lib/node_modules/npm/node_modules/bluebird/js/release/async.js:143:10)
9 verbose stack at Immediate.Async.drainQueues (/Users/bparks/.nvm/versions/node/v8.10.0/lib/node_modules/npm/node_modules/bluebird/js/release/async.js:17:14)
9 verbose stack at runCallback (timers.js:794:20)
9 verbose stack at tryOnImmediate (timers.js:752:5)
9 verbose stack at processImmediate [as _immediateCallback] (timers.js:729:5)
10 verbose cwd /Users/bparks/bin/node_scripts
11 verbose Darwin 17.5.0
12 verbose argv "/Users/bparks/.nvm/versions/node/v8.10.0/bin/node" "/Users/bparks/.nvm/versions/node/v8.10.0/bin/npm" "install" "@sindresorhus/slugify"
13 verbose node v8.10.0
14 verbose npm v5.6.0
15 error code E404
16 error 404 Not Found: @sindresorhus/slugify@latest
17 verbose exit [ 1, true ]
Have the same characters like 'Ö' and 'ö' in German and Turkish. How can we fix this?
It would be nice if this would support multiple occurrences of same headings, similar to what https://github.com/Flet/github-slugger can do. So multiple occurrences are appended with an incremental number or perhaps hash.
Thoughts @sindresorhus?
On latest Edge-18 I'm getting this error: "Expected identifier, string or number".
To see it, please go to https://x5e7o.csb.app/ with Edge-18 - this is the output from https://codesandbox.io/s/distracted-elgamal-x5e7o.
Basically it complains about the spread operators in slugify/index.js
. I don't see any supported platforms by this package, but could it be possible to support latest Edge-18 ?
What I expected:
slugify('Zürich', {
customReplacements: [["ä", "ae"], ["ö", "oe"], ["ü", "ue"], ["ß", "ss"]]
})
// -> result: zuerich
What I got instead:
// -> result: zurich
Reason:
string = deburr(string); <- will replace ü with u
string = decamelize(string);
string = doCustomReplacements(string, customReplacements);
Possible solution:
Run doCustomReplacements first.
string = doCustomReplacements(string, customReplacements);
string = deburr(string);
string = decamelize(string);
What do you think?
Why the CLI for this module is not included in the repo? It could be useful to have both interfaces in one place and doesn't really increase the module size.
For popular things that makes sense. Suggestions welcome, but I'm not going to add more than maybe 30. So make it worth it.
Don't do a PR. I'll add them all at once when we've decided on what to include.
The contractions/possessions removal (PR #71) causes unwanted behavior when it is invoked with a partial string which happens to end on a -s
or -t
.
In that case the -
is removed which might be unwanted if this library is used for live input transformation, eg in directus/directus#18352
For example the user starts to type article title
which gets transformed to article-t
as soon as the user types the t
, which then incorrectly gets turned into articlet
.
... Or at least don't work as I would expect them to work :)
Example:
import slugify from '@sindresorhus/slugify';
const customSlugify = string => slugify(string, {
customReplacements: [
['.', ''],
],
});
const slugified = customSlugify('x.y.z'); // Expected `xyz`, received `x-y-z`
I expected all dots to be converted into empty strings, so the expected result was xyz
but I received x-y-z
I suppose that it's not a bug, but it would be good if the documentation explained how custom replacements really work.
I'm getting an error when trying to import this into a Sanity project. I'm not sure whether there's something wrong with my config or whether theres something incompatible in the code.
I tried reinstalling v2.1.0
, v2.0.0
, `v1.1.0 but with the same results. Ended up using another package for now, but could help investigate. Have you had any other reports of something similar?
On node v14.17.6
Error in ./node_modules/@sindresorhus/slugify/index.js
Module parse failed: Unexpected token (35:2)
You may need an appropriate loader to handle this file type.
| preserveLeadingUnderscore: false,
| preserveTrailingDash: false,
| ...options
| };
Hi,
Does the library remove /
character from the path? I want to move certain pages as a sub-directory of main path, like integrations/abc
and then have an original integrations
page as well.
Is it possible with slufigy?
I've just noticed that the module is not transpiled to ES5, so Webpack requires additional setup:
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules\/(?!(@sindresorhus)\/).*/,
loader: 'babel-loader',
},
// ... other loaders
],
},
Otherwise, the module will not be transpiled by babel-loader which is not what the majority of users would expect.
If, for any reason, you don't want to ship the transpiled module, then I would, at least, add an info to the documentation that such setup is required.
Heya, great one.
Can we add options.transliterate
so we can disable transliteration? Defaulting to true
if you will so.
Line 46 in b841d04
to
string = options.transliterate ? transliterate(string, {customReplacements}) : string;
What the JAMstack means
becomes what-the-ja-mstack-means
Would be nice having this compatible as a ES2015 module for use in the browser
Hello,
Would it be possible to make the patternSlug
configurable, so for example if we want to have #
in our slug, we could do something like slugify('foo#bar hello$123', {patternSlug: /[^a-zA-Z#\d]+/g});
to get foo#bar-hello-123
.
Would be really amazing feature :-)
I want use it with chinese language, so can it support?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.