Skip to content

Commit

Permalink
fix: add some test
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrywu001 committed Mar 13, 2023
1 parent 1cda521 commit c30bc91
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 28 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Transform string to file in client side.

Playground: https://string-to-file.js-bridge.com

## Installation

```bash
Expand Down Expand Up @@ -34,7 +36,7 @@ const json = {
age: 12
};

const jsonFile = stringToFile(JSON.stringify(json, null, 2), 'xxx.json') as File;
const jsonFile = stringToFile(JSON.stringify(json, null, 2), 'xxx.json');

// download
saveFile(jsonFile);
Expand All @@ -49,7 +51,7 @@ const string = `Hello world!
How are you!`;

const stringFile = stringToFile(string, 'xxx.txt') as File;
const stringFile = stringToFile(string, 'xxx.txt');

// download
saveFile(stringFile);
Expand All @@ -64,7 +66,7 @@ const jsString = `const a = 'Hello world!';
console.log(a);`;

const jsFile = stringToFile(jsString, 'xxx.js') as File;
const jsFile = stringToFile(jsString, 'xxx.js');

// download
saveFile(jsFile);
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"file",
"string to file"
],
"homepage": "https://string-to-file.js-bridge.com",
"bugs": {
"url": "https://github.com/jerrywu001/string-to-file/issues"
},
Expand All @@ -33,7 +34,9 @@
"scripts": {
"build": "tsup",
"clean": "rimraf -rf ./node_modules && rimraf -rf playground/node_modules && rimraf -rf package-lock.json",
"dev": "concurrently \"npm run build:css-watch\" \"npm run dev -w playground\"",
"dev": "npm run dev -w playground",
"preview:dev": "npm run preview -w playground",
"build:dev": "npm run build -w playground",
"dev:pack": "npm run dev:usepack -w playground",
"lint": "eslint --ext .ts,.tsx ./src",
"prepare": "chmod a+x .husky/* && husky install",
Expand Down Expand Up @@ -62,7 +65,6 @@
"autoprefixer": "^10.4.14",
"babel-loader": "^9.1.2",
"bumpp": "^9.0.0",
"concurrently": "^7.6.0",
"core-js": "^3.29.0",
"esbuild-plugin-babel": "^0.2.3",
"eslint": "^8.36.0",
Expand Down
35 changes: 17 additions & 18 deletions playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@

<script setup lang="ts">
import hljs from 'highlight.js';
import { nextTick } from 'process';
import { stringToFile, saveFile, jsonToBase64, stringToBase64 } from 'string-to-file';
import { onMounted, ref } from 'vue';
import { onMounted, ref, nextTick } from 'vue';
import 'highlight.js/styles/github.css';
const json = { name: 'jack', age: 12 };
Expand Down Expand Up @@ -116,44 +115,44 @@ const doStringToBase64 = async () => {
};
const convert = async (type = 'json') => {
let str: any = null;
let file: File | null = null;
if (type === 'json') {
str = stringToFile(JSON.stringify(json, null, 2), 'test.json') as File;
file = stringToFile(JSON.stringify(json, null, 2), 'test.json');
result.value = hljs.highlight(JSON.stringify(getFileJson(str), null, 2), { language: 'json' }).value;
result.value = hljs.highlight(JSON.stringify(getFileJson(file), null, 2), { language: 'json' }).value;
}
if (type === 'string') {
str = stringToFile(string, 'test.txt') as File;
file = stringToFile(string, 'test.txt');
result.value = hljs.highlight(JSON.stringify(getFileJson(str), null, 2), { language: 'txt' }).value;
result.value = hljs.highlight(JSON.stringify(getFileJson(file), null, 2), { language: 'txt' }).value;
}
if (type === 'js') {
str = stringToFile(jsString, 'test.js') as File;
file = stringToFile(jsString, 'test.js');
result.value = hljs.highlight(JSON.stringify(getFileJson(str), null, 2), { language: 'javascript' }).value;
result.value = hljs.highlight(JSON.stringify(getFileJson(file), null, 2), { language: 'javascript' }).value;
}
console.log('Converted: ', str);
return str;
console.log('Converted: ', file);
return file;
};
const download = (type = 'json') => {
let str: any = null;
let file: File | null = null;
if (type === 'json') {
str = stringToFile(JSON.stringify(json, null, 2), 'test.json') as File;
saveFile(str);
file = stringToFile(JSON.stringify(json, null, 2), 'test.json');
saveFile(file);
}
if (type === 'string') {
str = stringToFile(string, 'test.txt') as File;
saveFile(str);
file = stringToFile(string, 'test.txt');
saveFile(file);
}
if (type === 'js') {
str = stringToFile(jsString, 'test.js') as File;
saveFile(str);
file = stringToFile(jsString, 'test.js');
saveFile(file);
}
};
Expand Down
2 changes: 0 additions & 2 deletions playground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
"jsx": "preserve",
"types": [
"vite/client",
"vite-plugin-pages/client",
"../global"
],
"paths": {
"~/*": [
Expand Down
58 changes: 55 additions & 3 deletions src/usage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,59 @@
import { expect } from 'vitest';
import {
base64ToFile,
fileToBlob,
fileToString,
jsonToBase64,
stringToBase64,
stringToFile,
} from '.';

describe('Ansi', () => {
test('can handle carriage symbol', () => {
expect(1).toBe(1);
const json = { name: 'jack', age: 12 };
const string = 'Hello world';
const jsString = 'const a = \'Hello world\';';

describe('string to file', () => {
test('can get converted file', () => {
const file = stringToFile(JSON.stringify(json, null, 2), 'test.json');
const txtFile = stringToFile(string, 'test.txt');
const jsFile = stringToFile(jsString, 'test.js');

expect(file instanceof File).toBeTruthy();
expect(file.name).toBe('test.json');

expect(txtFile.name).toBe('test.txt');
expect(jsFile.name).toBe('test.js');
});

test('file to blob', () => {
const file = stringToFile(JSON.stringify(json, null, 2), 'test.json');

expect(fileToBlob(file) instanceof Blob).toBeTruthy();
});

test('fileToString', async () => {
const file = stringToFile(string, 'test.txt');
const stringVal = await fileToString(file);

expect(stringVal).toEqual(string);
});

test('jsonToBase64', async () => {
const val = await jsonToBase64(json);

expect(val.startsWith('data:application/json;base64,')).toBeTruthy();
});

test('stringToBase64', async () => {
const val = await stringToBase64(string);

expect(val.includes('base64,')).toBeTruthy();
});

test('base64ToFile', async () => {
const base64 = await stringToBase64(string);
const file = base64ToFile(base64, 'test.txt');

expect(file.name).toBe('test.txt');
});
});

0 comments on commit c30bc91

Please sign in to comment.