Skip to content

Commit

Permalink
Create normalAndCensorStrings.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
wandyezj committed Oct 30, 2024
1 parent dd1c3e3 commit 3a147fb
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions code/typescript/normalAndCensorStrings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function placeholder(index: number) {
return `_____${index}_____`
}

function normalAndCensor(strings: string[], ...values: string[]): [string,string] {

const normal = strings.reduce((result, s, i) => `${result}${s}${values[i] || ""}`, "");
const censored = strings.reduce((result, s, i) => `${result}${s}${values[i] ? placeholder(i) : ""}`, "");
return [normal, censored]
}

function uncensor(s: string, values: string[]) {
return values.reduce((previous, current, index) => {
return previous.replace(placeholder(index), values[index]);
}, s)
}

const rootFolder = "Secret Content A";
const fileName = "Secret Content B";

const [normal, censor] = normalAndCensor`/me/drive/${rootFolder}/${fileName}:/content?@microsoft.graph.conflictBehavior=rename`;
const display = uncensor(censor, ["rootFolder", "fileName"]);

console.log(normal);
console.log(censor);
console.log(display);

// [LOG]: "/me/drive/Secret Content A/Secret Content B:/content?@microsoft.graph.conflictBehavior=rename"
// [LOG]: "/me/drive/_____0_____/_____1_____:/content?@microsoft.graph.conflictBehavior=rename"
// [LOG]: "/me/drive/rootFolder/fileName:/content?@microsoft.graph.conflictBehavior=rename"

0 comments on commit 3a147fb

Please sign in to comment.