Skip to content

Commit

Permalink
Add replace function
Browse files Browse the repository at this point in the history
  • Loading branch information
jen20 committed Nov 9, 2018
1 parent fbc9271 commit cadf732
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
16 changes: 10 additions & 6 deletions sdk/nodejs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ export function pow(base: number, exponent: number): number {
return Math.pow(base, exponent);
}

export function replace(input: string, toMatch: string | RegExp, replacement: string): string {
return input.replace(toMatch, replacement);
}

export function sha1(input: string): string {
const hash = crypto.createHash("sha1");
hash.update(input);
Expand Down Expand Up @@ -424,10 +428,6 @@ export function signum(input: number): number {
}
}

export function upper(input: string): string {
return input.toUpperCase();
}

export function slice(input: string[], start: number, end: number): string[] {
if (start < 0) {
throw new Error("start must be greater than or equal to 0");
Expand Down Expand Up @@ -506,14 +506,18 @@ export function trimspace(input: string): string {
return input.trim();
}

export function urlencode(input: string): string {
return encodeURIComponent(input);
export function upper(input: string): string {
return input.toUpperCase();
}

export function uuid(): string {
return v4().toString();
}

export function urlencode(input: string): string {
return encodeURIComponent(input);
}

export function values<T>(inputMap: { [k: string]: T }): T[] {
const keyList = keys(inputMap);

Expand Down
10 changes: 10 additions & 0 deletions sdk/nodejs/tests/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,16 @@ describe("pow", () => {
});
});

describe("replace", () => {
it("passes Terraform tests", () => {
assert.equal(sut.replace("hello", "hel", "bel"), "bello");
assert.equal(sut.replace("hello", "nope", "bel"), "hello");
assert.equal(sut.replace("hello", /l/g, "L"), "heLLo");
assert.equal(sut.replace("hello", /(l)/, "$1"), "hello");
assert.equal(sut.replace("helo", /(l)/, "$1$1"), "hello");
});
});

describe("sha1", () => {
it("passes Terraform tests", () => {
assert.equal(sut.sha1("test"), "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3");
Expand Down

0 comments on commit cadf732

Please sign in to comment.