Skip to content

Commit

Permalink
feat: encoder && decoder for twitter
Browse files Browse the repository at this point in the history
  • Loading branch information
Misaka-0x447f committed Sep 23, 2019
1 parent 2e0db0c commit 4edb972
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/social-network-provider/twitter.com/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SocialNetworkWorkerAndUIDefinition } from '../../social-network/shared'
import { usernameValidator } from './utils/user'
import { regexMatch } from '../../utils/utils'
import { batchReplace, regexMatch } from '../../utils/utils'
import { isNil } from 'lodash-es'

export const host = 'twitter.com'
export const hostURL = 'https://twitter.com'
Expand All @@ -16,5 +17,15 @@ export const sharedSettings: SocialNetworkWorkerAndUIDefinition = {
init() {},
publicKeyEncoder: (text: string) => `🎭${text}🎭`,
publicKeyDecoder: (text: string) => regexMatch(text, /(🎭)(.+)(🎭)/, 2),
payloadEncoder: (text: string) =>
`https://google.com/${batchReplace(text, [['🎼', '%20'], [':||', '%40'], ['+', '-'], ['=', '_'], ['|', '.']])}`,
payloadDecoder: (text: string) => {
let r = regexMatch(text, /https:\/\/google\.com\/%20(.+)%40/, 1)
if (isNil(r)) {
return 'null'
}
r = batchReplace(r, [['-', '+'], ['_', '='], ['.', '|']])
return `🎼${r}:||`
},
notReadyForProduction: true,
}
8 changes: 8 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,11 @@ export const regexMatch = (str: string, regexp: RegExp, index: number) => {
}

export const isDocument = (node: Node): node is Document => node.nodeType === Node.DOCUMENT_NODE

export const batchReplace = (source: string, group: Array<[string | RegExp, string]>) => {
let storage = source
for (const v of group) {
storage = storage.replace(v[0], v[1])
}
return storage
}

0 comments on commit 4edb972

Please sign in to comment.