Skip to content

Commit d869f2c

Browse files
authored
Merge pull request #106 from xsnippet/helpers-to-misc
Rename helpers to misc and change exports
2 parents 18ea8ff + 2294748 commit d869f2c

File tree

5 files changed

+18
-20
lines changed

5 files changed

+18
-20
lines changed

src/components/ListBoxWithSearch.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22

33
import ListBox from './ListBox';
4-
import { regExpEscape } from '../helpers';
4+
import * as misc from '../misc';
55

66
class ListBoxWithSearch extends React.PureComponent {
77
constructor(props) {
@@ -31,7 +31,7 @@ class ListBoxWithSearch extends React.PureComponent {
3131
// Filter out only those items that match search query. If no query is
3232
// set, do nothing and use the entire set.
3333
if (searchQuery) {
34-
const regExp = new RegExp(regExpEscape(searchQuery), 'gi');
34+
const regExp = new RegExp(misc.regExpEscape(searchQuery), 'gi');
3535
items = items.filter(item => item.name.match(regExp));
3636
}
3737

src/components/RecentSnippetItem.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import React from 'react';
22
import { Link } from 'react-router-dom';
33
import brace from 'brace';
44

5-
import { downloadSnippet, formatDate } from '../helpers';
5+
import * as misc from '../misc';
66

77
const RecentSnippetItem = ({ snippet }) => {
88
const { modesByName } = brace.acequire('ace/ext/modelist');
99
const mode = modesByName[snippet.get('syntax')] || modesByName.text;
1010
const syntax = mode.caption;
1111
const snippetTitle = snippet.get('title') || `#${snippet.get('id')}, Untitled`;
12-
const download = () => downloadSnippet(snippet);
12+
const download = () => misc.downloadSnippet(snippet);
1313
const rawUrl = process.env.RAW_SNIPPETS_URL_FORMAT.replace('%s', snippet.get('id'));
1414

1515
return (
@@ -21,7 +21,7 @@ const RecentSnippetItem = ({ snippet }) => {
2121
{snippet.get('tags').map(item => <span className="recent-snippet-meta-tag" key={item}>{item}</span>)}
2222
</div>
2323
</div>
24-
<span className="recent-snippet-meta-info">{formatDate(snippet.get('created_at'))}, by Guest</span>
24+
<span className="recent-snippet-meta-info">{misc.formatDate(snippet.get('created_at'))}, by Guest</span>
2525
</div>
2626
<div className="recent-snippet-actions">
2727
<span className="recent-snippet-lang">{syntax}</span>

src/components/Snippet.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import 'brace/theme/textmate';
77

88
import Spinner from './common/Spinner';
99
import * as actions from '../actions';
10-
import { downloadSnippet, copyToClipboard, formatDate } from '../helpers';
10+
import * as misc from '../misc';
1111

1212
import '../styles/Snippet.styl';
1313

@@ -18,7 +18,7 @@ export class Snippet extends React.Component {
1818
this.toggleEmbed = this.toggleEmbed.bind(this);
1919
this.download = this.download.bind(this);
2020
this.copyClipboard = (e) => {
21-
copyToClipboard(e, 'embedded');
21+
misc.copyToClipboard(e, 'embedded');
2222
};
2323
this.onEditorLoad = (editor) => {
2424
// we want to disable built-in find in favor of browser's one
@@ -34,7 +34,7 @@ export class Snippet extends React.Component {
3434
}
3535

3636
download() {
37-
downloadSnippet(this.props.snippet);
37+
misc.downloadSnippet(this.props.snippet);
3838
}
3939

4040
toggleEmbed() {
@@ -60,7 +60,7 @@ export class Snippet extends React.Component {
6060
<div className="snippet-data-tags">
6161
{snippet.get('tags').map(item => <span className="snippet-data-tag" key={item}>{item}</span>)}
6262
</div>
63-
<span className="snippet-data-meta">{formatDate(snippet.get('created_at'))}, by Guest</span>
63+
<span className="snippet-data-meta">{misc.formatDate(snippet.get('created_at'))}, by Guest</span>
6464
</div>
6565
<div className="snippet-data-actions">
6666
<span className="snippet-data-lang">{syntax}</span>

src/helpers/index.js renamed to src/misc/index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import brace from 'brace';
22
import 'brace/ext/modelist';
33

4-
const regExpEscape = string => string.replace(/[-[\]{}()*+?.,\\^$|]/g, '\\$&');
4+
export const regExpEscape = string => string.replace(/[-[\]{}()*+?.,\\^$|]/g, '\\$&');
55

6-
function download(text, name, mime) {
6+
export function download(text, name, mime) {
77
// It seems it's the only way to initiate file downloading from JavaScript
88
// as of Jan 7, 2018. If you read this and know a better way, please submit
99
// a pull request! ;)
@@ -19,7 +19,7 @@ function download(text, name, mime) {
1919
document.body.removeChild(element);
2020
}
2121

22-
function downloadSnippet(snippet) {
22+
export function downloadSnippet(snippet) {
2323
const { modesByName } = brace.acequire('ace/ext/modelist');
2424

2525
// Despite using AceEditor's modes as syntaxes, we can imagine other setup
@@ -36,18 +36,16 @@ function downloadSnippet(snippet) {
3636
download(content, name, 'text/plain');
3737
}
3838

39-
function copyToClipboard(e, id) {
39+
export function copyToClipboard(e, id) {
4040
document.getElementById(id).select();
4141
document.execCommand('copy');
4242
e.target.focus();
4343
}
4444

4545
// This function is here just because I don't want to pull the whole moment.js
4646
// only for one tiny date
47-
function formatDate(d) {
47+
export function formatDate(d) {
4848
const ISOdate = d.split('T')[0];
4949

5050
return ISOdate.split('-').reverse().join('.');
5151
}
52-
53-
export { regExpEscape, downloadSnippet, copyToClipboard, formatDate };

tests/helpers/index.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { formatDate } from '../../src/helpers';
1+
import * as misc from '../../src/misc';
22

3-
describe('helpers: formatDate', () => {
3+
describe('misc: formatDate', () => {
44
it('should return properly formated date', () => {
55
const incomeDate = '2018-03-11T15:28:19';
66

7-
expect(formatDate(incomeDate)).toEqual('11.03.2018');
7+
expect(misc.formatDate(incomeDate)).toEqual('11.03.2018');
88
});
99

1010
it('should return properly format7ed date for Jan', () => {
1111
const incomeDate = '2018-01-01T23:28:19';
1212

13-
expect(formatDate(incomeDate)).toEqual('01.01.2018');
13+
expect(misc.formatDate(incomeDate)).toEqual('01.01.2018');
1414
});
1515
});

0 commit comments

Comments
 (0)