-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from dojyorin/dev
update.
- Loading branch information
Showing
13 changed files
with
132 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
export {type JsonValue} from "https://deno.land/[email protected]/encoding/json/stream.ts"; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import "./test/base64.test.ts"; | ||
import "./test/date.test.ts"; | ||
import "./test/deflate.test.ts"; | ||
import "./test/fetch.test.ts"; | ||
import "./test/minipack.test.ts"; | ||
import "./test/minipack.test.ts"; | ||
import "./test/text.test.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export * from "./src/base64.ts"; | ||
export * from "./src/date.ts"; | ||
export * from "./src/deflate.ts"; | ||
export * from "./src/fetch.ts"; | ||
export * from "./src/minipack.ts"; | ||
export * from "./src/minipack.ts"; | ||
export * from "./src/text.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
/** | ||
* Convert from BASE64 code to byte array. | ||
* @param data The byte array. | ||
**/ | ||
* Convert from BASE64 code to byte. | ||
* @param data The byte. | ||
*/ | ||
export function base64Encode(data:Uint8Array){ | ||
return btoa([...data].map(n => String.fromCharCode(n)).join("")); | ||
} | ||
|
||
/** | ||
* Convert from byte array to BASE64 code. | ||
* Convert from byte to BASE64 code. | ||
* @param data The base64 code. | ||
**/ | ||
*/ | ||
export function base64Decode(data:string){ | ||
return new Uint8Array([...atob(data)].map(s => s.charCodeAt(0))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Convert from Date object to unix time. | ||
* Since the UnixTime that can be handled by the Date object is in milliseconds, this method output downscaled to 1/1000. | ||
* @param date The date object. If blank output the current time. | ||
*/ | ||
export function dateEncode(date?:Date){ | ||
return Math.floor((date ?? new Date()).getTime() / 1000); | ||
} | ||
|
||
/** | ||
* Convert from unix time to Date object. | ||
* Since the UnixTime that can be handled by the Date object is in milliseconds, the argument of this method is internally multiplied by x1000. | ||
* @param time The unix time. | ||
*/ | ||
export function dateDecode(time:number){ | ||
return new Date(time * 1000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
async function convert(data:Uint8Array, ts:TransformStream<Uint8Array, Uint8Array>){ | ||
async function streamConvert(data:Uint8Array, ts:TransformStream<Uint8Array, Uint8Array>){ | ||
return new Uint8Array(await new Response(new Blob([data]).stream().pipeThrough(ts)).arrayBuffer()); | ||
} | ||
|
||
/** | ||
* Compresses raw binary in "deflate" format (RFC1951 compliant). | ||
* It does not include header information like "gzip" (RFC1952) or "zlib" (RFC1950) as it does purely "compression only". | ||
* @param data The byte array. | ||
**/ | ||
* @param data The byte. | ||
*/ | ||
export async function deflateEncode(data:Uint8Array){ | ||
return await convert(data, new CompressionStream("deflate-raw")); | ||
return await streamConvert(data, new CompressionStream("deflate-raw")); | ||
} | ||
|
||
/** | ||
* Decompress "deflate" format (RFC1951 compliant) binary. | ||
* Binaries containing header information like "gzip" (RFC1952) or "zlib" (RFC1950) cannot be decompressed. | ||
* @param data The byte array. | ||
**/ | ||
* @param data The byte. | ||
*/ | ||
export async function deflateDecode(data:Uint8Array){ | ||
return await convert(data, new DecompressionStream("deflate-raw")); | ||
return await streamConvert(data, new DecompressionStream("deflate-raw")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* In addition to leading and trailing spaces, tabs, carriage returns, and two or more consecutive spaces are converted to a single space. | ||
* @param data The string. | ||
*/ | ||
export function trimExtend(data:string){ | ||
return data.trim().replace(/\r/g, "").replace(/\t/g, " ").replace(/ +/g, " ").replace(/ +$/mg, ""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {assertEquals} from "../deps.test.ts"; | ||
import {dateEncode, dateDecode} from "../src/date.ts"; | ||
|
||
const sample = new Date(2000, 0, 1, 0, 0, 0, 0); | ||
|
||
const encodeResult = 946684800; | ||
|
||
Deno.test({ | ||
name: "Date: Encode.", | ||
async fn(){ | ||
const result = await dateEncode(sample); | ||
|
||
assertEquals(result, encodeResult); | ||
} | ||
}); | ||
|
||
Deno.test({ | ||
name: "Date: Decode.", | ||
async fn(){ | ||
const result = await dateDecode(encodeResult); | ||
|
||
assertEquals(result.toISOString(), sample.toISOString()); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {assertEquals} from "../deps.test.ts"; | ||
import {trimExtend} from "../src/text.ts"; | ||
|
||
const sample = " Lorem ipsum\r dolor sit \t amet. "; | ||
|
||
const encodeResult = "Lorem ipsum dolor sit amet."; | ||
|
||
Deno.test({ | ||
name: "Text: Trim.", | ||
async fn(){ | ||
const result = await trimExtend(sample); | ||
|
||
assertEquals(result, encodeResult); | ||
} | ||
}); |