0
\$\begingroup\$

I have created an Angular pipe to suppress the sensitive information like credit cards, bank account, ABA numbers etc.

This is working fine but I would like to know if this is the best possible way to implement the logic.

Here is the Typescript code for pipe logic.

export class SuppressInfoPipe implements PipeTransform {
 transform(valueToSupress: string, unSuppressedCount?: number): string {
 let suppressedOutput = '';
 const valueToRemainUnsuppressed =
 valueToSupress.substring(valueToSupress.length - unSuppressedCount, valueToSupress.length);
 let astariskLength = valueToSupress.length - unSuppressedCount;
 for ( let i = 0; i < astariskLength; i++) {
 suppressedOutput = suppressedOutput.concat('*');
 }
 suppressedOutput = suppressedOutput.concat(valueToRemainUnsuppressed);
 return suppressedOutput;
 }
}

it takes the string input and the number of character they will no be hidden and then return the suppressed output.

Comments and suggestions are welcomed.

200_success
146k22 gold badges190 silver badges478 bronze badges
asked Mar 14, 2019 at 8:47
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I would avoid the for loop to generate the "suppressed string".

My approach would be:

export class SuppressInfoPipe implements PipeTransform {
 transform(valueToSupress: string, unSuppressedCount = 0): string {
 const suppressedCount = valueToSupress.length - unSuppressedCount;
 const valueToRemainUnsuppressed =
 valueToSupress.substring(suppressedCount, valueToSupress.length);
 return Array(suppressedCount + 1).join('*') + valueToRemainUnsuppressed; // suppressedCount + 1: since join will a string of length "suppressedCount"
 }
}

In this case:

Array(n) will return an array of length n.

enter image description here

.join("*") will join the list and return a string equivalent of length n-1.

answered Mar 19, 2019 at 9:20
\$\endgroup\$
3
  • \$\begingroup\$ That's much better. Only thing I want to confirm is this changing the type of Array object when returning just like JavaScript does to variables depending upon the data being assigned ? \$\endgroup\$ Commented Mar 19, 2019 at 11:13
  • \$\begingroup\$ Updated my answer. \$\endgroup\$ Commented Mar 19, 2019 at 11:18
  • \$\begingroup\$ I got your point :) . Thanks for adding explanation to your answer. \$\endgroup\$ Commented Mar 19, 2019 at 11:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.