Skip to content

Instantly share code, notes, and snippets.

View semlinker's full-sized avatar
👊
Fighting

阿宝哥 semlinker

👊
Fighting
View GitHub Profile
@semlinker
semlinker / index.html
Last active July 16, 2023 03:59
JavaScript 中如何实现大文件并发上传?
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>大文件并发上传示例(阿宝哥)</title>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/spark-md5/3.0.0/spark-md5.min.js"></script>
</head>
@semlinker
semlinker / transmat-source.html
Created July 3, 2021 04:33
Transmat 使用示例
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Transmat Source 示例</title>
<script src="https://unpkg.com/transmat/lib/index.umd.js"></script>
<style>
body {
@semlinker
semlinker / index.html
Created July 24, 2022 09:21
File Type Detect Demo
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Type Detect Demo</title>
</head>
<body>
<div>
function asyncPool(concurrency, iterable, iteratorFn) {
let i = 0;
const ret = []; // // Store all asynchronous tasks
const executing = new Set(); // Stores executing asynchronous tasks
const enqueue = function() {
if (i === iterable.length) {
return Promise.resolve();
}
const item = iterable[i++]; // Get the new task item
const p = Promise.resolve().then(() => iteratorFn(item, iterable));
@semlinker
semlinker / async-pool-es7.js
Last active March 20, 2023 18:21
async-pool-es7
async function asyncPool(concurrency, iterable, iteratorFn) {
const ret = []; // Store all asynchronous tasks
const executing = new Set(); // Stores executing asynchronous tasks
for (const item of iterable) {
// Call the iteratorFn function to create an asynchronous task
const p = Promise.resolve().then(() => iteratorFn(item, iterable));
ret.push(p); // save new async task
executing.add(p); // Save an executing asynchronous task
@semlinker
semlinker / promise.all.js
Created July 25, 2022 03:57
Promise.all
Promise.all = function (iterators) {
return new Promise((resolve, reject) => {
if (!iterators || iterators.length === 0) {
resolve([]);
} else {
// used to determine whether all tasks are completed
let count = 0;
let result = []; // result array
for (let i = 0; i < iterators.length; i++) {
// Considering that iterators[i] may be ordinary object,
@semlinker
semlinker / promise.race.js
Created July 25, 2022 03:58
Promise.race
Promise.race = function (iterators) {
return new Promise((resolve, reject) => {
for (const iter of iterators) {
Promise.resolve(iter)
.then((res) => {
resolve(res);
})
.catch((e) => {
reject(e);
});
@semlinker
semlinker / get-binary-content.js
Created July 25, 2022 10:27
Get Binary Content
function getBinaryContent(url, start, end, i) {
return new Promise((resolve, reject) => {
try {
let xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.setRequestHeader("range", `bytes=${start}-${end}`); // Set range request information
xhr.responseType = "arraybuffer"; // Set the returned type to arraybuffer
xhr.onload = function () {
resolve({
index: i, // file block index
@semlinker
semlinker / concatenate.js
Created July 25, 2022 10:32
Concatenate Function
function concatenate(arrays) {
if (!arrays.length) return null;
let totalLength = arrays.reduce((acc, value) => acc + value.length, 0);
let result = new Uint8Array(totalLength);
let length = 0;
for (let array of arrays) {
result.set(array, length);
length += array.length;
}
return result;
@semlinker
semlinker / get-content-length.js
Created July 25, 2022 10:34
Get content length
function getContentLength(url) {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open("HEAD", url);
xhr.send();
xhr.onload = function () {
resolve(
~~xhr.getResponseHeader("Content-Length")
);
};