Commit 398c8929 authored by Mark Stenglein's avatar Mark Stenglein

Adds a bunch of new stuff

- Adds a parameter class
- Adds a potential ParameterValue interface(?)
- Adds some new utils
- THIS WILL FAIL ON CI, srry I need to go to bed, so I'm committing
  what I've got.

- From here:
  - Test cases need to be written for the new parameter class
  - Test cases need to be written for the two new utils
  - Test cases need to be written for the changes to content line
parent 63321b97
Pipeline #914 failed with stage
in 13 seconds
import { isAlpha } from "./util";
import { isIamaToken, isAlpha } from "./util";
import Parameter from "./Parameter";
const CRLF: string = "/r/n"
/**
* Implementation of a Content Line from RFC 5545
......@@ -15,10 +17,10 @@ import { isAlpha } from "./util";
*/
export default class ContentLine {
private _name: string;
private _params: string[] = [];
private _params: Parameter[] = [];
private _value: string;
constructor(inName: string, inParams: string[], inValue: string) {
constructor(inName: string, inParams: Parameter[], inValue: string) {
this.name = inName;
this._params = inParams;
this.value = inValue;
......@@ -30,7 +32,7 @@ export default class ContentLine {
return this._name;
}
get params(): string[] {
get params(): Parameter[] {
return this._params;
}
......@@ -41,7 +43,7 @@ export default class ContentLine {
/* Setters */
set name(newName: string) {
if (isAlpha(newName)) {
if (isIamaToken(newName)) {
this._name = newName;
}
else {
......@@ -63,7 +65,7 @@ export default class ContentLine {
* TODO: Make sure that this handles multi-octed UTF-8 segments properly.
*/
static fold(line: string): string {
return line.match(/(.{1,74})/g).join("\r\n ");
return line.match(/(.{1,74})/g).join(CRLF + " ");
}
/**
......@@ -79,7 +81,7 @@ export default class ContentLine {
outputLine += param;
});
outputLine += ":" + this.value + "\r\n";
outputLine += ":" + this.value + CRLF;
return ContentLine.fold(outputLine);
}
......
import { isAlpha, isIamaToken, isXName } from "./util";
import { ParameterValue } from "./ParameterValue";
/**
* Implementation of a Content Line Parameter from RFC 5545
*
* Chapter 3.1 defines the general ruleset for a parameter. This implementation
* will utilize an abstract Class which is meant to be extended by specific
* types of parameters.
*
* @author Mark Stenglein <mark@stengle.in>
*/
export default class Parameter {
private _paramName: string;
private _paramValues: string[] | Date[];
/**
* Constructor builds the Parameter from the parameter name and an array of
* values.
*
* @author Mark Stenglein
* @since 0.1.0
* @param inName string The name of the new Parameter in iama-token / x-name
* @param inValues string[] | Date[] array of values
*/
constructor(inName: string, inValues: string[] | Date[]) {
this.paramName = inName;
this.paramValues = inValues
}
/** Get Methods */
get paramName() {
return this._paramName;
}
get paramValues() {
return this.paramValues;
}
/** Set Methods */
/**
* Validates input param names and saves them to the object.
*
* @author Mark Stenglein <mark@stengle.in>
* @since 0.1.0
* @param newName The new name to be tested and saved.
* @returns void
* @throws TypeError if newName is not a valid iCal name.
*/
set paramName(newName: string) {
/**
* Note that the X-Name also passes these rules, the X-Name can be
* defined separately.
*/
if (isIamaToken(newName)) {
this._paramName = newName;
}
else {
throw new TypeError("Parameter must be valid iama-token or x-token");
}
}
/**
* Validates input param values and saves them to the object.
*
* @author
* @since 0.1.0
* @param newValues The input values to be tested and saved.
*/
set paramValues(newValues: string[] | Date[]) {
this._paramValues = newValues;
}
/**
* Generates the proper string representation for a Parameter as defined by
* RFC 5545:
*
* param = param-name "=" param-value *("," param-value)
*
* @author Mark Stenglein
* @since 0.1.0
* @returns string Representation of the Parameter as defined in RFC 5545
*/
public generate(): string {
let outputString = this.paramName;
/**
* Goes through each parameter value and adds it, being sure to place
* the comma for the 2nd parameter onwards.
*/
for (let i = 0; i < this.paramValues.length; i++) {
if (i !== 0) {
outputString += ",";
}
outputString += this.paramValues[i];
}
return outputString;
}
}
export interface ParameterValue {
toString(): string;
}
\ No newline at end of file
......@@ -15,3 +15,42 @@
export function isAlpha(input: string): boolean {
return /^[a-zA-Z]+$/.test(input);
}
/**
* Checks to see if the input string is a compliant iama-token
*
* Definition of iama-token from the spec:
*
* iana-token = 1*(ALPHA / DIGIT / "-")
* ; iCalendar identifier registered with IANA
*
* @author Mark Stenglein <mark@stengle.in>
* @since 0.1.0
* @param input Input string to be tested
* @returns boolean value of if it is a valid token
*/
export function isIamaToken(input: string): boolean {
return /^[a-zA-Z0-9-]+$/.test(input);
}
/**
* Checks to see if the input string is a compliant x-token
*
* Definition of x-name from the spec:
*
* x-name = "X-" [vendorid "-"] 1*(ALPHA / DIGIT / "-")
* ; Reserved for experimental use.
*
* vendorid = 3*(ALPHA / DIGIT)
* ; Vendor identification
*
* @author Mark Stenglein <mark@stengle.in>
* @since 0.1.0
* @param input Input string to be tested
* @returns boolean value of if input is a valid experimental token
*
* TODO: Implement this!
*/
export function isXName(input: string): boolean {
return true;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment