-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from simple-ssi/jrayback_240520_refactor-funct…
…ional-patterns refactor functional patterns
- Loading branch information
Showing
131 changed files
with
1,220 additions
and
784 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,24 +1,10 @@ | ||
import { Buffer } from 'buffer/index.js' | ||
import { pipe } from '../../lib/util/pipe.js' | ||
import { Code } from '../../core/code/code.js' | ||
import { Binary } from '../../core/domain/binary.js' | ||
import { encodeText } from './lib/encodeText.js' | ||
import { encodeBinary } from './lib/encodeBinary.js' | ||
import { extractRawFromRaw } from './lib/extractRawFromRaw.js' | ||
import { Binary } from '../../core/domain/domains/binary.js' | ||
import { convertToByteArray } from './steps/convertToByteArray.js' | ||
import { text as makeText } from './text.js' | ||
|
||
// encodes a primitive in the Binary domain. needs the type code and the primitive as a byte array | ||
|
||
export const binary = (code: Code, primitive: Uint8Array): Binary => { | ||
// helper functions | ||
const makeRaw = extractRawFromRaw(code) | ||
const asText = encodeText(code) | ||
const asBinary = encodeBinary | ||
|
||
// for Binary, go through all three steps... | ||
return pipe( | ||
Buffer.from(primitive), // internally, we use Buffers | ||
makeRaw, // make the Raw | ||
asText, // encode the raw primitive as Text | ||
asBinary // return the encoded string as a Binary | ||
) | ||
} | ||
export const binary = (code: Code, primitive: Uint8Array): Binary => pipe( | ||
makeText(code, primitive), | ||
convertToByteArray | ||
) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,10 +1,6 @@ | ||
import { Buffer } from 'buffer/index.js' | ||
import { make } from '../../implementation/make.js' | ||
import { Code } from '../../core/code/code.js' | ||
import { Raw } from '../../core/domain/raw.js' | ||
import { Raw } from '../../core/domain/domains/raw.js' | ||
|
||
export const raw = (code: Code, primitive: Uint8Array): Raw => | ||
make( // make() ensures the primitive is valididated | ||
code, | ||
Buffer.from(primitive) | ||
) | ||
export const raw = (code: Code, primitive: Uint8Array): Raw => make(code, Buffer.from(primitive)) // internally, we use Buffers |
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,18 @@ | ||
import { Buffer } from 'buffer/index.js' | ||
import { PadSize, padSize as ps } from '../../../lib/keri/padSize.js' | ||
import { exhaustive } from '../../../lib/util/exhaustive.js' | ||
|
||
const prependWithNothing = (primitive: Buffer): Buffer => primitive | ||
const prependWithZero = (primitive: Buffer): Buffer => Buffer.concat([Buffer.from([0]), primitive]) | ||
const prependWithTwoZeros = (primitive: Buffer): Buffer => Buffer.concat([Buffer.from([0, 0]), primitive]) | ||
|
||
export const addPadding = (primitive: Buffer): Buffer => { | ||
const padSize: PadSize = ps(primitive.length) | ||
return padSize === 0 | ||
? prependWithNothing(primitive) | ||
: padSize === 1 | ||
? prependWithZero(primitive) | ||
: padSize === 2 | ||
? prependWithTwoZeros(primitive) | ||
: exhaustive(padSize) | ||
} |
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 @@ | ||
import { Buffer } from 'buffer/index.js' | ||
import { base64UrlToBuffer } from '../../../lib/convertors/base64UrlToBuffer.js' | ||
|
||
export const convertToByteArray = (base64Url: string): Buffer => base64UrlToBuffer(base64Url) |
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 @@ | ||
import { Buffer } from 'buffer/index.js' | ||
import { bufferToBase64URL } from '../../../lib/convertors/bufferToBase64URL.js' | ||
|
||
export const convertToUrlSafeBase64 = (bytes: Buffer): string => bufferToBase64URL(bytes) |
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,19 @@ | ||
import { Code, CodeLength } from '../../../core/code/code.js' | ||
import { exhaustive } from '../../../lib/util/exhaustive.js' | ||
|
||
type Swapper = (text: string) => string | ||
|
||
const replaceFirstCharacter = (code: Code, base64: string): string => code + base64.substring(1) | ||
const replaceFirstTwoCharacters = (code: Code, base64: string): string => code + base64.substring(2) | ||
const simplyPrependFourCharacterCode = (code: Code, base64: string): string => code + base64 | ||
|
||
export const swapInTextCode = (code: Code): Swapper => { | ||
const length = code.length as CodeLength | ||
return length === 1 | ||
? (base64: string) => replaceFirstCharacter(code, base64) | ||
: length === 2 | ||
? (base64: string) => replaceFirstTwoCharacters(code, base64) | ||
: length === 4 | ||
? (base64: string) => simplyPrependFourCharacterCode(code, base64) | ||
: exhaustive(length) // ensure exhaustiveness | ||
} |
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 |
---|---|---|
@@ -1,19 +1,16 @@ | ||
import { Buffer } from 'buffer/index.js' | ||
import { pipe } from '../../lib/util/pipe.js' | ||
import { Code } from '../../core/code/code.js' | ||
import { Text } from '../../core/domain/text.js' | ||
import { encodeText } from './lib/encodeText.js' | ||
import { extractRawFromRaw } from './lib/extractRawFromRaw.js' | ||
import { Text } from '../../core/domain/domains/text.js' | ||
import { make as makeRaw } from '../../implementation/make.js' | ||
import { addPadding } from './steps/addPadding.js' | ||
import { convertToUrlSafeBase64 } from './steps/convertToUrlSafeBase64.js' | ||
import { swapInTextCode } from './steps/swapInTextCode.js' | ||
|
||
export const text = (code: Code, primitive: Uint8Array): Text => { | ||
// helper functions | ||
const makeRaw = extractRawFromRaw(code) | ||
const asText = encodeText(code) | ||
|
||
// requires two steps... | ||
return pipe( | ||
Buffer.from(primitive), // convert bytes to Buffer -> internally, we use Buffers | ||
makeRaw, // make the Raw first, then... | ||
asText // ...encode the it as Text | ||
export const text = (code: Code, primitive: Uint8Array): Text => | ||
pipe( | ||
makeRaw(code, primitive) | ||
.raw, // call make() to ensure the primitive is valid, then re-extract the primitive | ||
addPadding, | ||
convertToUrlSafeBase64, | ||
swapInTextCode(code) | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 @@ | ||
import { Buffer } from 'buffer/index.js' | ||
import { base64UrlToBuffer } from '../../../lib/convertors/base64UrlToBuffer.js' | ||
|
||
export const convertToByteArray = (base64: string): Buffer => base64UrlToBuffer(base64) |
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,5 @@ | ||
import { Code } from '../../../core/code/code.js' | ||
import { Raw } from '../../../core/domain/domains/raw.js' | ||
import { make } from '../../../implementation/make.js' | ||
|
||
export const produceTheRaw = (code: Code) => (primitive: Buffer): Raw => make(code, primitive) |
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,19 @@ | ||
import { readCodeFromText } from './readCodeFromText.js' | ||
|
||
describe('Read Code From Text', () => { | ||
it('handles code length of one', () => { | ||
const text = 'MGrj' | ||
const actual = readCodeFromText(text) | ||
expect(actual).toStrictEqual('M') | ||
}) | ||
it('handles code length of two', () => { | ||
const text = '0HGrjk23' | ||
const actual = readCodeFromText(text) | ||
expect(actual).toStrictEqual('0H') | ||
}) | ||
it('handles code length of four', () => { | ||
const text = '1AAFRkJZ' | ||
const actual = readCodeFromText(text) | ||
expect(actual).toStrictEqual('1AAF') | ||
}) | ||
}) |
26 changes: 10 additions & 16 deletions
26
src/api/transform/lib/readCodeFromText.ts → src/api/transform/steps/readCodeFromText.ts
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 |
---|---|---|
@@ -1,27 +1,21 @@ | ||
// import { match } from 'ts-pattern' | ||
import { Text } from '../../../core/domain/text.js' | ||
import { Text } from '../../../core/domain/domains/text.js' | ||
import { Code } from '../../../core/code/code.js' | ||
import { exhaustive } from '../../../lib/util/exhaustive.js' | ||
import { Selector } from '../../../core/selector/selector.js' | ||
|
||
const readOneCharCode = (text: Text): Code => text[0] as Code | ||
const readTwoCharCode = (text: Text): Code => text.substring(0, 2) as Code | ||
const readFourCharCode = (text: Text): Code => text.substring(0, 4) as Code | ||
|
||
// reads the code characters from the Text | ||
// returns a transitional object to be further processed | ||
export const readCodeFromText = (textDomain: Text): Code => { | ||
const numChars = textDomain[0].match(/[A-Za-z]/) != null | ||
? 1 | ||
: textDomain[0] === '0' | ||
? 2 | ||
: 4 | ||
switch (numChars) { | ||
case 1: | ||
return readOneCharCode(textDomain) | ||
case 2: | ||
return readTwoCharCode(textDomain) | ||
case 4: | ||
return readFourCharCode(textDomain) | ||
} | ||
return exhaustive(numChars) | ||
const selector = textDomain[0] as Selector | ||
return selector !== '0' && selector !== '1' | ||
? readOneCharCode(textDomain) | ||
: selector === '0' | ||
? readTwoCharCode(textDomain) | ||
: selector === '1' | ||
? readFourCharCode(textDomain) | ||
: exhaustive(selector) | ||
} |
8 changes: 4 additions & 4 deletions
8
src/api/transform/lib/trimBytes.test.ts → ...api/transform/steps/removePadding.test.ts
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
Oops, something went wrong.