Skip to content

Commit

Permalink
feat: add maskerPipe
Browse files Browse the repository at this point in the history
closes #4
  • Loading branch information
pismenskiy authored Mar 4, 2020
1 parent 759ec10 commit 231d6ed
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 10 deletions.
4 changes: 4 additions & 0 deletions customTypings/fast-luhn/index.d.ts
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
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"dependencies": {
"@qiwi/substrate": "^1.18.11",
"@types/lodash.once": "^4.1.6",
"fast-luhn": "^1.0.4",
"lodash.once": "^4.1.1",
"safe-json-stringify": "^1.2.0",
"tslib": "^1.11.1"
Expand Down
1 change: 1 addition & 0 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { createHttpPipe } from './pipes/http'
export { createMaskerPipe, panMaskerPipe } from './pipes/masker'
export { createTransmitter, createTransmittable } from './transmitter'

export * from './interfaces'
Expand Down
28 changes: 28 additions & 0 deletions src/main/ts/pipes/masker.ts
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)
22 changes: 22 additions & 0 deletions src/main/ts/utils/deepmap.ts
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)
}
1 change: 1 addition & 0 deletions src/main/ts/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { deepMap } from './deepmap'
4 changes: 2 additions & 2 deletions src/test/ts/pipes/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ describe('httpPipe', () => {

it('returns remote data if succeeds', () => {
const httpPipe = createHttpPipe({ url: 'https://reqres.in/api/users/2', method: HttpMethod.GET })
const transittable: ITransmittable = { data: null, meta: { history: [] } }
const transmittable: ITransmittable = { data: null, meta: { history: [] } }

return expect(httpPipe.execute(transittable, noop))
return expect(httpPipe.execute(transmittable, noop))
.resolves.toEqual([null, {
data: {
id: 2,
Expand Down
7 changes: 0 additions & 7 deletions src/test/ts/pipes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ describe('pipes', () => {
}
}

/* const pipeline1: TPipeline = [foo, [upper]]
const [err, data] = await execute(createTransmittable({
message: 'bar'
}), pipeline1)
console.log(err, data) */

const pipeline2: TPipeline = [upper, foo]

const [err, data] = await execute(createTransmittable({
Expand Down
47 changes: 47 additions & 0 deletions src/test/ts/pipes/masker.ts
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']]
]
])
})
})
51 changes: 51 additions & 0 deletions src/test/ts/utils/deepmap.ts
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)
})
})
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
"esModuleInterop": true,

"incremental": true,
"tsBuildInfoFile": "./buildcache/.tsbuildinfo"
"tsBuildInfoFile": "./buildcache/.tsbuildinfo",
"typeRoots": [
"./typings",
"customTypings",
"node_modules/@types"
]
},
"include": [
"src/main/**/*"
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2749,6 +2749,11 @@ fast-levenshtein@~2.0.6:
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=

fast-luhn@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/fast-luhn/-/fast-luhn-1.0.4.tgz#7de2df0c6f868e4d23b4f3a162633d81fc5a50c1"
integrity sha512-TukISTtdVBSykwFKdXWHNRQWaezelkjpyx+pDkTOKM9r5vnYUlcblYpsA5GLfNpqSj9gXEOVNA43OwqPDK/7Og==

fastq@^1.6.0:
version "1.6.1"
resolved "https://registry.yarnpkg.com/fastq/-/fastq-1.6.1.tgz#4570c74f2ded173e71cf0beb08ac70bb85826791"
Expand Down

0 comments on commit 231d6ed

Please sign in to comment.