-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
168 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
declare module 'fast-luhn' { | ||
const luhn: (el: string) => string; | ||
export = luhn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import luhn from 'fast-luhn' | ||
import { IPipe } from '../interfaces' | ||
import { deepMap } from '../utils' | ||
const type = 'masker' | ||
|
||
export const createMaskerPipe = ( | ||
fn: (el: any, key?: string) => {} | ||
): IPipe => ({ | ||
type, | ||
async execute ({ data }) { | ||
try { | ||
const maskedData = deepMap(data, fn) | ||
return [null, maskedData] | ||
} catch (e) { | ||
return [e, null] | ||
} | ||
} | ||
}) | ||
|
||
export const panMaskerFn = (input: string | number): string => { | ||
return (input + '').replace(/\d{13,19}/g, v => | ||
luhn(v) | ||
? `${v.slice(0, 4)} **** **** ${v.slice(-4)}` | ||
: '' + input | ||
) | ||
} | ||
|
||
export const panMaskerPipe = createMaskerPipe(panMaskerFn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export function deepMap ( | ||
input: any, | ||
fn: (input: Record<string, any>, key?: string) => any, | ||
refs = new WeakMap(), | ||
key?: string | ||
) { | ||
if (typeof input === 'object' && input !== null) { | ||
const ref = refs.get(input) | ||
if (ref) { | ||
return ref | ||
} | ||
const n: Record<string, any> = Array.isArray(input) ? [] : {} | ||
refs.set(input, n) | ||
for (const i in input) { | ||
if (Object.prototype.hasOwnProperty.call(input, i)) { | ||
n[i] = deepMap(input[i], fn, refs, i) | ||
} | ||
} | ||
return n | ||
} | ||
return fn(input, key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { deepMap } from './deepmap' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { | ||
createMaskerPipe, | ||
panMaskerPipe, | ||
ITransmittable | ||
} from '../../../main/ts' | ||
|
||
const noop = () => { /* noop */ } | ||
|
||
describe('maskerPipe', () => { | ||
it('factory returns IPipe', () => { | ||
const maskerPipe = createMaskerPipe(el => el) | ||
|
||
expect(maskerPipe.type).toBe('masker') | ||
expect(maskerPipe.execute).toEqual(expect.any(Function)) | ||
}) | ||
|
||
it('return masked elements', async () => { | ||
const maskerPipe = createMaskerPipe(el => el.toString() + 'masked') | ||
const transmittable: ITransmittable = { | ||
data: ['foo', 'bar', ['foo2', ['foo3']]], | ||
meta: { history: [] } | ||
} | ||
expect(await maskerPipe.execute(transmittable, noop)).toStrictEqual([ | ||
null, | ||
['foomasked', 'barmasked', ['foo2masked', ['foo3masked']]] | ||
]) | ||
}) | ||
|
||
it('return masked elements', async () => { | ||
const transmittable: ITransmittable = { | ||
data: [ | ||
'4111111111111111', | ||
'bar', | ||
['4111111111111111', ['foo3', '0000000000000000']] | ||
], | ||
meta: { history: [] } | ||
} | ||
expect(await panMaskerPipe.execute(transmittable, noop)).toStrictEqual([ | ||
null, | ||
[ | ||
'4111 **** **** 1111', | ||
'bar', | ||
['4111 **** **** 1111', ['foo3', '0000 **** **** 0000']] | ||
] | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { deepMap } from '../../../main/ts/utils' | ||
|
||
describe('deepMap', () => { | ||
it('handle object', () => { | ||
const testObj = { | ||
a: 1, | ||
b: [ | ||
1, | ||
2, | ||
{ | ||
c: 4, | ||
d: { | ||
e: 6, | ||
f: [7, 8] | ||
} | ||
} | ||
] | ||
} | ||
|
||
expect(deepMap(testObj, el => Number(el) * 10)).toMatchObject({ | ||
a: 10, | ||
b: [ | ||
10, | ||
20, | ||
{ | ||
c: 40, | ||
d: { | ||
e: 60, | ||
f: [70, 80] | ||
} | ||
} | ||
] | ||
}) | ||
}) | ||
|
||
it('handle circular deps', () => { | ||
const testObj: Record<string, any> = { | ||
a: 1, | ||
b: 2 | ||
} | ||
testObj.foo = testObj | ||
|
||
const resObj: Record<string, any> = { | ||
a: 10, | ||
b: 20 | ||
} | ||
resObj.foo = resObj | ||
|
||
expect(deepMap(testObj, el => Number(el) * 10)).toMatchObject(resObj) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters