Commit 5e2350cd authored by Himanshu Kapoor's avatar Himanshu Kapoor

Take object as parameter instead of 3 params

This allows passing the file object directly and makes the function
call more verbose.
parent e6f2fad3
...@@ -18,7 +18,7 @@ export default { ...@@ -18,7 +18,7 @@ export default {
return getFileEOL(this.activeFile.content); return getFileEOL(this.activeFile.content);
}, },
activeFileIsText() { activeFileIsText() {
return isTextFile(this.activeFile.name, this.activeFile.content); return isTextFile(this.activeFile);
}, },
}, },
}; };
......
...@@ -28,7 +28,7 @@ export default { ...@@ -28,7 +28,7 @@ export default {
const { name } = file; const { name } = file;
const encodedContent = target.result.split('base64,')[1]; const encodedContent = target.result.split('base64,')[1];
const rawContent = encodedContent ? atob(encodedContent) : ''; const rawContent = encodedContent ? atob(encodedContent) : '';
const isText = isTextFile(name, rawContent, file.type); const isText = isTextFile({ content: rawContent, mimeType: file.type, name });
const emitCreateEvent = content => const emitCreateEvent = content =>
this.$emit('create', { this.$emit('create', {
......
...@@ -60,7 +60,7 @@ export default { ...@@ -60,7 +60,7 @@ export default {
]), ]),
...mapGetters('fileTemplates', ['showFileTemplatesBar']), ...mapGetters('fileTemplates', ['showFileTemplatesBar']),
shouldHideEditor() { shouldHideEditor() {
return this.file && !isTextFile(this.file.name, this.file.content); return this.file && !isTextFile(this.file);
}, },
showContentViewer() { showContentViewer() {
return ( return (
......
...@@ -42,8 +42,8 @@ const KNOWN_TYPES = [ ...@@ -42,8 +42,8 @@ const KNOWN_TYPES = [
}, },
]; ];
export function isTextFile(fileName, content, mimeType = '') { export function isTextFile({ name, content, mimeType = '' }) {
const knownType = KNOWN_TYPES.find(type => type.isMatch(mimeType, fileName)); const knownType = KNOWN_TYPES.find(type => type.isMatch(mimeType, name));
if (knownType) return knownType.isText; if (knownType) return knownType.isText;
......
...@@ -13,78 +13,78 @@ import { ...@@ -13,78 +13,78 @@ import {
describe('WebIDE utils', () => { describe('WebIDE utils', () => {
describe('isTextFile', () => { describe('isTextFile', () => {
it('returns false for known binary types', () => { it.each`
expect(isTextFile('my.png', 'file content', 'image/png')).toBeFalsy(); mimeType | name | type | result
// mime types are case insensitive ${'image/png'} | ${'my.png'} | ${'binary'} | ${false}
expect(isTextFile('my.png', 'file content', 'IMAGE/PNG')).toBeFalsy(); ${'IMAGE/PNG'} | ${'my.png'} | ${'binary'} | ${false}
}); ${'text/plain'} | ${'my.txt'} | ${'text'} | ${true}
${'TEXT/PLAIN'} | ${'my.txt'} | ${'text'} | ${true}
it('returns true for known text types', () => { `('returns $result for known $type types', ({ mimeType, name, result }) => {
expect(isTextFile('my.txt', 'file content', 'text/plain')).toBeTruthy(); expect(isTextFile({ content: 'file content', mimeType, name })).toBe(result);
// mime types are case insensitive
expect(isTextFile('my.txt', 'file content', 'TEXT/PLAIN')).toBeTruthy();
});
it('returns true for file extensions that Monaco supports syntax highlighting for', () => {
// test based on both MIME and extension
expect(isTextFile('my.json', '{"éêė":"value"}', 'application/json')).toBeTruthy();
expect(isTextFile('.tsconfig', '{"éêė":"value"}', 'application/json')).toBeTruthy();
expect(isTextFile('my.sql', 'SELECT "éêė" from tablename', 'application/sql')).toBeTruthy();
}); });
it('returns true even irrespective of whether the mimes, extensions or file names are lowercase or upper case', () => { it.each`
expect(isTextFile('MY.JSON', '{"éêė":"value"}', 'application/json')).toBeTruthy(); content | mimeType | name
expect(isTextFile('MY.SQL', 'SELECT "éêė" from tablename', 'application/sql')).toBeTruthy(); ${'{"éêė":"value"}'} | ${'application/json'} | ${'my.json'}
expect( ${'{"éêė":"value"}'} | ${'application/json'} | ${'.tsconfig'}
isTextFile('Gruntfile', 'var code = "something"', 'application/javascript'), ${'SELECT "éêė" from tablename'} | ${'application/sql'} | ${'my.sql'}
).toBeTruthy(); ${'{"éêė":"value"}'} | ${'application/json'} | ${'MY.JSON'}
expect( ${'SELECT "éêė" from tablename'} | ${'application/sql'} | ${'MY.SQL'}
isTextFile( ${'var code = "something"'} | ${'application/javascript'} | ${'Gruntfile'}
'dockerfile', ${'MAINTAINER Александр "a21283@me.com"'} | ${'application/octet-stream'} | ${'dockerfile'}
'MAINTAINER Александр "alexander11354322283@me.com"', `(
'application/octet-stream', 'returns true for file extensions that Monaco supports syntax highlighting for',
), ({ content, mimeType, name }) => {
).toBeTruthy(); expect(isTextFile({ content, mimeType, name })).toBe(true);
}); },
);
it('returns false if filename is same as the expected extension', () => { it('returns false if filename is same as the expected extension', () => {
expect(isTextFile('sql', 'SELECT "éêė" from tablename', 'application/sql')).toBeFalsy(); expect(
isTextFile({
name: 'sql',
content: 'SELECT "éêė" from tablename',
mimeType: 'application/sql',
}),
).toBeFalsy();
}); });
it('returns true for ASCII only content for unknown types', () => { it('returns true for ASCII only content for unknown types', () => {
expect(isTextFile('hello.mytype', 'plain text', 'application/x-new-type')).toBeTruthy();
});
it('returns true for relevant filenames', () => {
expect( expect(
isTextFile( isTextFile({
'Dockerfile', name: 'hello.mytype',
'MAINTAINER Александр "alexander11354322283@me.com"', content: 'plain text',
'application/octet-stream', mimeType: 'application/x-new-type',
), }),
).toBeTruthy(); ).toBeTruthy();
}); });
it('returns false for non-ASCII content for unknown types', () => { it('returns false for non-ASCII content for unknown types', () => {
expect(isTextFile('my.random', '{"éêė":"value"}', 'application/octet-stream')).toBeFalsy(); expect(
isTextFile({
name: 'my.random',
content: '{"éêė":"value"}',
mimeType: 'application/octet-stream',
}),
).toBeFalsy();
}); });
it.each` it.each`
filename | result name | result
${'myfile.txt'} | ${true} ${'myfile.txt'} | ${true}
${'Dockerfile'} | ${true} ${'Dockerfile'} | ${true}
${'img.png'} | ${false} ${'img.png'} | ${false}
${'abc.js'} | ${true} ${'abc.js'} | ${true}
${'abc.random'} | ${false} ${'abc.random'} | ${false}
${'image.jpeg'} | ${false} ${'image.jpeg'} | ${false}
`('returns $result for $filename', ({ filename, result }) => { `('returns $result for $filename when no content or mimeType is passed', ({ name, result }) => {
expect(isTextFile(filename)).toBe(result); expect(isTextFile({ name })).toBe(result);
}); });
it('returns true if content is empty string but false if content is not passed', () => { it('returns true if content is empty string but false if content is not passed', () => {
expect(isTextFile('abc.dat')).toBe(false); expect(isTextFile({ name: 'abc.dat' })).toBe(false);
expect(isTextFile('abc.dat', '')).toBe(true); expect(isTextFile({ name: 'abc.dat', content: '' })).toBe(true);
expect(isTextFile('abc.dat', ' ')).toBe(true); expect(isTextFile({ name: 'abc.dat', content: ' ' })).toBe(true);
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment