Skip to content

Instantly share code, notes, and snippets.

@liuderchi
Last active September 8, 2017 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuderchi/8116126979c044bc78947d71fd90f991 to your computer and use it in GitHub Desktop.
Save liuderchi/8116126979c044bc78947d71fd90f991 to your computer and use it in GitHub Desktop.
Insert React Functional Component Snippet for Atom
// steps:
// 1. copy this file to ~/.atom/init.js
// 2. in Atom, dispatch command "Window:Reload", Atom would run this file rather than init.coffee
// 3. create new empty file Foo.js
// 4. focus to new tab, send command "react:insert-functional-component-template"
atom.commands.add('atom-text-editor', 'react:insert-functional-component-template', () => {
editor = atom.workspace.getActiveTextEditor()
if (!editor) { return null }
const fileNameCap = getCamelCaseNameFromPath(editor.getPath())
const template = `import React from 'react'\n\nconst ${fileNameCap} = (props) => {\n return (\n <div></div>\n )\n}\n\nexport default ${fileNameCap}`
editor.getLastSelection().insertText(template)
})
const getCamelCaseNameFromPath = (filePath) => {
const [ _, filenameNoExt ] = /(.*?)(?:\.[^.]+)?$/.exec(path.basename(filePath))
return filenameNoExt
.replace(/[\W_]/g, ' ')
.replace(/\w+/g, match => match[0].toUpperCase() + match.slice(1))
.replace(/\s+/g, '')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment