Skip to content

Commit e42bdba

Browse files
committed
🐛 with cycle(), rotate()
1 parent 5cda7f7 commit e42bdba

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nodef/extra-string",
3-
"version": "0.2.2",
3+
"version": "0.2.3",
44
"license": "AGPL-3.0",
55
"exports": "./index.ts",
66
"exclude": [".github/"]

index.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ export const MAX_CODE_POINT: number = 0x10FFFF;
4343
function IDENTITY<T>(v: T): T {
4444
return v;
4545
}
46+
47+
48+
function mod(x: number, y: number): number {
49+
return x - y * Math.floor(x/y);
50+
}
4651
//#endregion
4752

4853

@@ -332,6 +337,7 @@ export function substring(x: string, start: number, end?: number): string {
332337
export function split(x: string, separator?: string | RegExp, limit?: number): string[] {
333338
return x.split(separator as string | RegExp, limit);
334339
}
340+
// splitBy(x: string, separator: function, limit?: number): string[]?
335341
//#endregion
336342

337343

@@ -1385,6 +1391,7 @@ export function toWords(x: string, re: RegExp | null=null): string[] {
13851391
}
13861392
return words;
13871393
}
1394+
// NOTE: Shouldn't this be called splitWords() or splitIntoWords() or splitIntoTokens()?
13881395

13891396

13901397
/**
@@ -1504,34 +1511,34 @@ export {toSlugCase as slugify};
15041511

15051512

15061513

1507-
//#region TRANSFORM (TODO)
1514+
//#region TRANSFORM
15081515
/**
15091516
* Get characters that cycle through string.
15101517
* @param x a string
15111518
* @param start start index
15121519
* @param count number of characters [length]
15131520
*/
1514-
// function cycle(x: string, start: number, count: number=x.length): string {
1515-
// let X = x.length;
1516-
// if(count<=0 || X===0) return "";
1517-
// let start = index(x, start);
1518-
// let a = x.slice(start, start+count);
1519-
// count -= a.length;
1520-
// for(let m=0, M=Math.floor(count/X); m<M; m++)
1521-
// a += x;
1522-
// return a += x.slice(0, count % X);
1523-
// }
1521+
export function cycle(x: string, start: number, count: number=x.length): string {
1522+
if (!x || count<=0) return "";
1523+
let a = x.slice(start, start + count);
1524+
count -= a.length; // start = index(x, start);
1525+
const M = Math.floor(count / x.length);
1526+
for (let m=0; m<M; m++)
1527+
a += x;
1528+
return a += x.slice(0, count % x.length);
1529+
}
15241530

15251531

15261532
/**
15271533
* Rotate characters in string.
15281534
* @param x a string
15291535
* @param n rotate amount (+ve: left, -ve: right)
15301536
*/
1531-
// function rotate(x: string, n: number): string {
1532-
// let i = mod(n, x.length);
1533-
// return x.slice(i) + x.slice(0, i);
1534-
// }
1537+
export function rotate(x: string, n: number): string {
1538+
const i = mod(n, x.length);
1539+
return x.slice(i) + x.slice(0, i);
1540+
}
1541+
15351542

15361543
// function replacePrefix(str, pre, rep) {
15371544
// return str.startsWith(pre)? rep+str.substr(pre.length):str;
@@ -1559,7 +1566,6 @@ export {toSlugCase as slugify};
15591566

15601567
// swapRange()
15611568

1562-
15631569
// import {cutAt as arrayCut} from "extra-array";
15641570

15651571
/**
@@ -1582,8 +1588,6 @@ export {toSlugCase as slugify};
15821588
// return arrayCutRight(x as any, is as any) as any;
15831589
// }
15841590

1585-
1586-
15871591
// function unionCompare(x: string, y: string, fn: any=null): string {
15881592
// // let fn = fn||cmp;
15891593
// let s = new Set<string>();

0 commit comments

Comments
 (0)