-
Notifications
You must be signed in to change notification settings - Fork 5
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 #11 from evert/serialize
Serialize
- Loading branch information
Showing
21 changed files
with
415 additions
and
61 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import Parser from './parser'; | ||
import Serializer from './serializer'; | ||
import { Dictionary, Item, List, ListList, ParameterizedList } from './types'; | ||
|
||
module.exports = { | ||
|
||
parseDictionary: (input: string): Dictionary => { | ||
|
||
const parser = new Parser(input); | ||
return parser.parseDictionary(); | ||
|
||
}, | ||
|
||
parseList: (input: string): List => { | ||
|
||
const parser = new Parser(input); | ||
return parser.parseList(); | ||
|
||
}, | ||
|
||
parseListList: (input: string): ListList => { | ||
|
||
const parser = new Parser(input); | ||
return parser.parseListList(); | ||
|
||
}, | ||
|
||
parseParamList: (input: string): ParameterizedList => { | ||
|
||
const parser = new Parser(input); | ||
return parser.parseParamList(); | ||
|
||
}, | ||
|
||
parseItem: (input: string): Item => { | ||
|
||
const parser = new Parser(input); | ||
return parser.parseItem(); | ||
|
||
}, | ||
|
||
serializeDictionary: (input: Dictionary): string => { | ||
|
||
const serializer = new Serializer(); | ||
return serializer.serializeDictionary(input); | ||
|
||
}, | ||
|
||
serializeList: (input: List) => { | ||
|
||
const serializer = new Serializer(); | ||
return serializer.serializeList(input); | ||
|
||
}, | ||
|
||
serializeListList: (input: ListList) => { | ||
|
||
const serializer = new Serializer(); | ||
return serializer.serializeListList(input); | ||
|
||
}, | ||
|
||
serializeParamList: (input: ParameterizedList) => { | ||
|
||
const serializer = new Serializer(); | ||
return serializer.serializeParamList(input); | ||
|
||
}, | ||
|
||
serializeItem: (input: Item) => { | ||
|
||
const serializer = new Serializer(); | ||
return serializer.serializeItem(input); | ||
|
||
}, | ||
|
||
}; |
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,127 @@ | ||
import { Dictionary, Item, List, ListList, ParameterizedList } from './types'; | ||
|
||
export default class Serializer { | ||
|
||
serializeDictionary(input: Dictionary): string { | ||
|
||
const output = []; | ||
for (const [key, value] of Object.entries(input)) { | ||
output.push(this.serializeKey(key) + '=' + this.serializeItem(value)); | ||
} | ||
|
||
return output.join(', '); | ||
|
||
} | ||
|
||
serializeList(input: List): string { | ||
|
||
return input.map(this.serializeItem.bind(this)).join(', '); | ||
|
||
} | ||
|
||
serializeListList(input: ListList): string { | ||
|
||
return input.map( | ||
innerList => innerList.map(this.serializeItem.bind(this)).join('; ') | ||
).join(', '); | ||
|
||
} | ||
|
||
serializeParamList(input: ParameterizedList): string { | ||
|
||
const output = []; | ||
for (const [key, dict] of input) { | ||
let item = ''; | ||
item += this.serializeKey(key); | ||
|
||
for (const [dictKey, dictValue] of Object.entries(dict)) { | ||
item += ';' + this.serializeKey(dictKey); | ||
if (dictValue) { | ||
item += '=' + this.serializeItem(dictValue); | ||
} | ||
} | ||
|
||
output.push(item); | ||
} | ||
return output.join(', '); | ||
|
||
} | ||
|
||
serializeKey(input: string): string { | ||
|
||
if (!/^[a-z][a-z0-9_-]*$/.test(input)) { | ||
throw new Error('Dictionary keys must start with a-z and only contain a-z0-9_-'); | ||
} | ||
|
||
return input; | ||
|
||
} | ||
|
||
serializeItem(input: Item): string { | ||
|
||
if (typeof input === 'number') { | ||
if (Number.isInteger(input)) { | ||
return this.serializeInt(input); | ||
} | ||
return this.serializeFloat(input); | ||
} | ||
if (typeof input === 'string') { | ||
return this.serializeString(input); | ||
} | ||
// Token ??? | ||
if (typeof input === 'boolean') { | ||
return this.serializeBoolean(input); | ||
} | ||
if (input instanceof Buffer) { | ||
return this.serializeByteSequence(input); | ||
} | ||
throw new Error('Cannot serialize values of type ' + typeof input); | ||
|
||
} | ||
|
||
serializeInt(input: number): string { | ||
if (input > 999_999_999_999_999 || input < -999_999_999_999_999) { | ||
throw new Error('Integers may not be larger than 15 digits'); | ||
} | ||
return input.toString(); | ||
} | ||
|
||
serializeFloat(input: number): string { | ||
|
||
return input.toString(); | ||
|
||
} | ||
|
||
serializeString(input: string): string { | ||
|
||
if (!/^[\x1F-\x7F]*$/.test(input)) { | ||
throw new Error('Strings must be in the ASCII range'); | ||
} | ||
|
||
return '"' + input.replace('"', '\\"') + '"'; | ||
|
||
} | ||
|
||
serializeToken(input: string): string { | ||
|
||
if (!/^[a-zA-Z][a-zA-Z0-9_\-\.\:\%\*]*$/.test(input)) { | ||
throw new Error('Tokens must start with a letter and must only contain A-Za-z_-.:%*/'); | ||
} | ||
|
||
return input; | ||
|
||
} | ||
|
||
serializeByteSequence(input: Buffer): string { | ||
|
||
return '*' + input.toString('base64') + '*'; | ||
|
||
} | ||
|
||
serializeBoolean(input: boolean): string { | ||
|
||
return input ? '?1' : '?0'; | ||
|
||
} | ||
|
||
} |
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,11 @@ | ||
export type Item = string | number | Buffer | boolean; | ||
|
||
export type Dictionary = { | ||
[s: string]: Item | ||
}; | ||
|
||
export type List = Item[]; | ||
export type ListList = List[]; | ||
|
||
export type ParameterizedIdentifier = [string, Dictionary]; | ||
export type ParameterizedList = ParameterizedIdentifier[]; |
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,4 @@ | ||
--recursive | ||
--require ts-node/register | ||
--exit | ||
test/*.js |
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
Oops, something went wrong.