Skip to content

Commit

Permalink
Create the actual lib
Browse files Browse the repository at this point in the history
  • Loading branch information
martpie committed Nov 19, 2018
1 parent 83cd5f3 commit 837b781
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dist

# Logs
logs
*.log
Expand Down
89 changes: 89 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "react-slugify",
"version": "1.0.0",
"description": "Slugify a React node",
"main": "dist/index.js",
"scripts": {
"prepare": "npm run build",
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/martpie/react-slugify.git"
},
"keywords": [
"react",
"slug",
"slugify"
],
"author": "Pierre de la Martiniere <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/martpie/react-slugify/issues"
},
"homepage": "https://github.com/martpie/react-slugify#readme",
"devDependencies": {
"@types/react": "^16.7.6",
"react": "^16.6.3",
"typescript": "^3.1.6"
}
}
47 changes: 47 additions & 0 deletions src/slugify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as React from 'react';

const harmonize = (text: string) => text
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^a-z-]/g, '');

const slugify = (node: React.ReactNode): string => {
// undefined, null, boolean
if (!node || typeof node === 'boolean') {
return '';
}

// string, number
if (typeof node === 'string') return harmonize(node);
if (typeof node === 'number') return String(node);

// empty object
if (typeof node === 'object' && Object.keys(node).length === 0) {
return '';
}

// We did the check about empty object before
// const castedNode = node as React.ReactElement<any> | React.ReactNodeArray | React.ReactPortal;

// ReactPortal
if ('children' in node) {
return slugify(node.children);
}

// ReactNodeArray
if (node instanceof Array) {
return node.map(n => slugify(n)).join('-');
}


// ReactElement
if ('type' in node) {
// Would be awesome to uglify(children), but I am not sure how
return '';
}

// unhandled case
return '';
};

export default slugify;
21 changes: 21 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compileOnSave": false,
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"jsx": "preserve",
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": false,
"preserveConstEnums": true,
"sourceMap": true,
"strict": true,
"outDir": "dist",
"declaration": true
},
"exclude": [
"**/node_modules",
"**/__tests__/**"
]
}

0 comments on commit 837b781

Please sign in to comment.