Skip to content

Commit

Permalink
Merge pull request #67 from dojyorin/dev
Browse files Browse the repository at this point in the history
feat: zero padding.
  • Loading branch information
dojyorin authored Jul 27, 2023
2 parents 65dc775 + a544146 commit 3baa15e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
8 changes: 4 additions & 4 deletions deps.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {assertEquals} from "https://deno.land/std@0.195.0/testing/asserts.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.195.0/path/mod.ts";
export {serve} from "https://deno.land/std@0.195.0/http/mod.ts";
export {exists} from "https://deno.land/std@0.195.0/fs/mod.ts";
export {assertEquals} from "https://deno.land/std@0.196.0/testing/asserts.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.196.0/path/mod.ts";
export {serve} from "https://deno.land/std@0.196.0/http/mod.ts";
export {exists} from "https://deno.land/std@0.196.0/fs/mod.ts";
8 changes: 4 additions & 4 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {dirname, fromFileUrl} from "https://deno.land/std@0.195.0/path/mod.ts";
export {Logger} from "https://deno.land/std@0.195.0/log/mod.ts";
export {ConsoleHandler, FileHandler} from "https://deno.land/std@0.195.0/log/handlers.ts";
export {format as formatDate} from "https://deno.land/std@0.195.0/datetime/mod.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.196.0/path/mod.ts";
export {Logger} from "https://deno.land/std@0.196.0/log/mod.ts";
export {ConsoleHandler, FileHandler} from "https://deno.land/std@0.196.0/log/handlers.ts";
export {format as formatDate} from "https://deno.land/std@0.196.0/datetime/mod.ts";
2 changes: 1 addition & 1 deletion src/process.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Run command as subprocess.
* @example
* ```ts
* const success = await runCommand(["echo", "foobar"]);
* const success = await runCommand("echo", "foobar");
* ```
*/
export async function runCommand(...commands:string[]):Promise<boolean>{
Expand Down
17 changes: 15 additions & 2 deletions src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function utfDecode(data:Uint8Array):string{
* ```
*/
export function hexEncode(data:Uint8Array):string{
return [...data].map(n => n.toString(16).toUpperCase().padStart(2, "0")).join("");
return [...data].map(n => pad0(n, 2, 16)).join("");
}

/**
Expand All @@ -47,7 +47,7 @@ export function hexEncode(data:Uint8Array):string{
* ```
*/
export function hexDecode(data:string):Uint8Array{
return new Uint8Array(data.match(/[0-9a-fA-F]{2}/g)?.map(s => parseInt(s, 16)) ?? []);
return new Uint8Array(data.match(/[0-9a-fA-F]{2}/g)?.map(s => Number(`0x${s}`)) ?? []);
}

/**
Expand Down Expand Up @@ -125,4 +125,17 @@ export function cleanText(data:string){
*/
export function accurateSegment(data:string):string[]{
return [...new Intl.Segmenter().segment(data)].map(({segment}) => segment);
}

/**
* Create string with zero padding at beginning of number.
* Output is 2 digits by default.
* @example
* ```ts
* const num = 8;
* const padding = pad0(num);
* ```
*/
export function pad0(data:number, digit?:number, radix?:number){
return data.toString(radix).toUpperCase().padStart(digit ?? 2, "0");
}
1 change: 0 additions & 1 deletion test/process.deno.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {assertEquals} from "../deps.test.ts";
import {runCommand} from "../src/process.deno.ts";

Deno.test({
ignore: Deno.build.os !== "windows",
name: "Process: Run (no args)",
async fn(){
const result = await runCommand("echo", "abcdefg");
Expand Down
11 changes: 10 additions & 1 deletion test/text.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {assertEquals} from "../deps.test.ts";
import {utfEncode, utfDecode, hexEncode, hexDecode, trimExtend, fixWidth, cleanText, accurateSegment} from "../src/text.ts";
import {utfEncode, utfDecode, hexEncode, hexDecode, trimExtend, fixWidth, cleanText, accurateSegment, pad0} from "../src/text.ts";

const sampleText = " Lorem ipsum\r dolor sit \r\r amet. ";
const sampleBin = new Uint8Array([
Expand Down Expand Up @@ -64,4 +64,13 @@ Deno.test({

assertEquals(length, 5);
}
});

Deno.test({
name: "Text: Pad 0",
fn(){
const pad = pad0(8);

assertEquals(pad, "08");
}
});

0 comments on commit 3baa15e

Please sign in to comment.