immutable-ics
Advanced tools
Comparing version 0.1.0 to 0.2.0
{ | ||
"name": "immutable-ics", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Immutable iCalendar document creation", | ||
@@ -14,2 +14,8 @@ "main": "src/index.js", | ||
}, | ||
"keywords": [ | ||
"ics", | ||
"calendar", | ||
"ical", | ||
"immutable" | ||
], | ||
"author": "Angelo Ashmore", | ||
@@ -23,3 +29,2 @@ "license": "ISC", | ||
"immutable": "facebook/immutable-js#4.0", | ||
"left-pad": "^1.1.0", | ||
"lodash": "^4.13.1" | ||
@@ -35,2 +40,8 @@ }, | ||
}, | ||
"babel": { | ||
"presets": [ | ||
"es2015", | ||
"stage-0" | ||
] | ||
}, | ||
"standard": { | ||
@@ -37,0 +48,0 @@ "parser": "babel-eslint" |
189
README.md
# immutable-ics | ||
Immutable iCalendar document creation | ||
Immutable iCalendar document creation using [Immutable.js][0]. | ||
## Status | ||
[![npm version](https://badge.fury.io/js/immutable-ics.svg)](http://badge.fury.io/js/immutable-ics) | ||
[![Build Status](https://secure.travis-ci.org/angeloashmore/immutable-ics.svg?branch=master)](http://travis-ci.org/angeloashmore/immutable-ics?branch=master) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) | ||
## Installation | ||
```sh | ||
npm install --save immutable-ics | ||
``` | ||
## Quick Guide | ||
### Import | ||
Import the modules: | ||
```js | ||
import { Component, Property } from 'immutable-ics' | ||
``` | ||
The following modules are available: | ||
* `Component`: Immutable `Record` to build an iCalendar Component | ||
* `Property`: Immutable `Record` to build an iCalendar Property | ||
### Create a component | ||
Create a new component and add properties: | ||
```js | ||
let calendar | ||
let event | ||
const versionProperty = new Property({ name: 'VERSION', value: 2 }) | ||
const dtstartProperty = new Property({ | ||
name: 'DTSTART', | ||
parameters: { VALUE: 'DATE' }, | ||
value: new Date('1991-07-11 7:00:00') | ||
}) | ||
calendar = new Component({ name: 'VCALENDAR' }) | ||
calendar = calendar.addProperty(versionProperty) | ||
event = new Component({ name: 'VEVENT' }) | ||
event = event.addProperty(dtstartProperty) | ||
calendar = calendar.addComponent(event) | ||
``` | ||
Or instantiate everything at once: | ||
```js | ||
const calendar = new Component({ | ||
name: 'VCALENDAR', | ||
properties: [ | ||
new Property({ name: 'VERSION', value: 2 }) | ||
], | ||
components: [ | ||
new Component({ | ||
name: 'VEVENT', | ||
properties: [ | ||
new Property({ | ||
name: 'DTSTART', | ||
parameters: { VALUE: 'DATE' }, | ||
value: new Date('1991-07-11 7:00:00') | ||
}) | ||
] | ||
}) | ||
] | ||
}) | ||
``` | ||
### Generate iCalendar data | ||
Call `#toString` on the `Component` or `Property` to get a string representation | ||
of the component according to the iCalendar specifications. | ||
``` | ||
calendar.toString() | ||
``` | ||
Generated data: | ||
``` | ||
BEGIN:VCALENDAR | ||
VERSION:2.0 | ||
BEGIN:VEVENT | ||
DTSTART;VALUE=DATE:19910711 | ||
END:VEVENT | ||
END:VCALENDAR | ||
``` | ||
This string can then be saved to a file, sent to the user, etc. | ||
## API | ||
### `Component` | ||
An Immutable `Record` with the following properties: | ||
* `name: String`: Name of the component (e.g. "VCALENDAR", "VEVENT") | ||
* `components: List`: List of `Component` instances | ||
* `properties: List`: List of `Property` instances | ||
All methods return a new instance of the component due to its backing on | ||
Immutable's `Record`. | ||
#### Methods | ||
* **`Component.constructor({ name: String, components: (List | Array), properties: (List | Array) }): Component`** | ||
Instantiate a new `Component` with initial values. `components` and | ||
`properties` will be coerced into a `List`. | ||
* **`Component.prototype.pushComponent(component: Component): Component`** | ||
Push a `Component` to the list of components. | ||
* **`Component.prototype.pushProperty(property: Property): Component`** | ||
Push a `Property` to the list of properties. | ||
* **`Component.prototype.clear(): Component`** | ||
Clear all components and properties from the component. | ||
* **`Component.prototype.clearComponents(): Component`** | ||
Clear all components from the component. | ||
* **`Component.prototype.clearProperties(): Component`** | ||
Clear all properties from the component. | ||
* **`Component.prototype.toString(): String`** | ||
Get a string representation of the component according to the iCalendar | ||
specifications. | ||
### `Property` | ||
An Immutable `Record` with the following properties: | ||
* `name: String`: Name of the property (e.g. "DTSTART", "SUMMARY") | ||
* `parameters: Map`: Property parameters (e.g. "VALUE") | ||
* `transform: Boolean`: Explicit determiner if the value is transformed | ||
* `value: Any`: Value of the property | ||
All methods return a new instance of the property due to its backing on | ||
Immutable's `Record`. | ||
#### Methods | ||
* **`Property.constructor({ name: String, parameters: (Object | Map), transform: Boolean, value: Any }): Property`** | ||
Instantiate a new `Property` with initial values. `parameters` will be coerced | ||
into a `Map`. | ||
* **`Property.prototype.getTransformedValue(): String`** | ||
Get the transformed value of the property's value. Transformations are | ||
conveniences to generate valid iCalendar data from JavaScript objects. | ||
For example, providing a `Date` object to a `DTSTAMP` property value will | ||
transform as such: | ||
```js | ||
const dtstampProperty = new Property({ | ||
name: 'DTSTAMP' | ||
parameters: { VALUE: 'DATE' }, | ||
value: new Date('1991-07-11 7:00:00') | ||
}) | ||
dtstampProperty.toString() // => DTSTAMP;VALUE=DATE:19910711 | ||
``` | ||
* **`Property.prototype.toString(): String`** | ||
Get a string representation of the property according to the iCalendar | ||
specifications. | ||
[0]: /facebook/immutable-js/ |
@@ -1,2 +0,2 @@ | ||
import leftpad from 'left-pad' | ||
import { padStart } from 'lodash' | ||
@@ -11,4 +11,4 @@ /** | ||
return date.getFullYear() + | ||
leftpad(date.getMonth() + 1, 2, 0) + | ||
leftpad(date.getDate(), 2, 0) | ||
padStart(date.getMonth() + 1, 2, 0) + | ||
padStart(date.getDate(), 2, 0) | ||
} |
@@ -1,2 +0,2 @@ | ||
import leftpad from 'left-pad' | ||
import { padStart } from 'lodash' | ||
@@ -10,5 +10,5 @@ /** | ||
export default (date) => { | ||
return leftpad(date.getHours(), 2, 0) + | ||
leftpad(date.getMinutes(), 2, 0) + | ||
leftpad(date.getSeconds(), 2, 0) | ||
return padStart(date.getHours(), 2, 0) + | ||
padStart(date.getMinutes(), 2, 0) + | ||
padStart(date.getSeconds(), 2, 0) | ||
} |
import { Map, Record } from 'immutable' | ||
import { identity, isFunction, isNull, isUndefined } from 'lodash' | ||
import { identity, isArray, isFunction, isNull, isUndefined } from 'lodash' | ||
import * as transformers from './transformers' | ||
@@ -26,13 +26,17 @@ import { | ||
if (isFunction(transformers[valueType])) { | ||
return transformers[valueType](this) || this.value | ||
} else { | ||
const transformer = transformers[valueType] | ||
if (!isFunction(transformer)) { | ||
return this.value | ||
} | ||
if (isArray(this.value)) { | ||
return this.value.map((item) => transformer(item, this.parameters)) | ||
.join(',') | ||
} | ||
return transformer(this.value, this.parameters) | ||
} | ||
toString () { | ||
const value = this.transform ? this.getTransformedValue() : this.value | ||
// Build the output. | ||
let string = this.name | ||
@@ -48,2 +52,4 @@ | ||
const value = this.transform ? this.getTransformedValue() : this.value | ||
if (!isNull(value) && !isUndefined(value)) { | ||
@@ -50,0 +56,0 @@ string += PROPERTY_KV_SEPARATOR + value |
@@ -7,27 +7,32 @@ import { isDate } from 'lodash' | ||
export const BOOLEAN = ({ value }) => value ? 'TRUE' : 'FALSE' | ||
export const BOOLEAN = (value) => value ? 'TRUE' : 'FALSE' | ||
export const DATE = ({ value }) => { | ||
if (isDate(value)) { | ||
return formatDate(removeTimeZoneOffset(value)) | ||
} | ||
} | ||
export const DATE = (value) => ( | ||
isDate(value) ? formatDate(removeTimeZoneOffset(value)) | ||
: value | ||
) | ||
export const DATETIME = ({ value }) => { | ||
if (isDate(value)) { | ||
return formatDateTime(value) | ||
} | ||
} | ||
export const DATETIME = (value) => ( | ||
isDate(value) ? formatDateTime(value) | ||
: value | ||
) | ||
export const FLOAT = ({ value }) => Number.parseFloat(value) | ||
export const FLOAT = (value) => Number.parseFloat(value) | ||
// Special case to force VERSION property to be a float with one decimal place. | ||
export const FLOAT__FIXED_1 = (property) => FLOAT(property).toFixed(1) | ||
export const FLOAT__FIXED_1 = (...args) => FLOAT(...args).toFixed(1) | ||
export const INTEGER = ({ value }) => Number.parseInt(value) | ||
export const INTEGER = (value) => Number.parseInt(value) | ||
export const TIME = ({ value }) => { | ||
if (isDate(value)) { | ||
return formatTime(removeTimeZoneOffset(value)) | ||
} | ||
} | ||
export const TEXT = (value) => ( | ||
value.toString() | ||
.replace('\\', '\\\\') | ||
.replace(',', '\\,') | ||
.replace(';', '\\;') | ||
.replace('\n', '\\n') | ||
) | ||
export const TIME = (value) => ( | ||
isDate(value) ? formatTime(removeTimeZoneOffset(value)) | ||
: value | ||
) |
@@ -23,2 +23,3 @@ const string = 'BEGIN:VCALENDAR\r\n' + | ||
' haracter limit per the RFC)\r\n' + | ||
'DESCRIPTION:Test character escaping: \\n \\, \\; \\\\\r\n' + | ||
'CATEGORIES:WORK,FAMILY\r\n' + | ||
@@ -25,0 +26,0 @@ 'END:VTODO\r\n' + |
@@ -61,3 +61,9 @@ /* eslint-env mocha */ | ||
new Property({ name: 'DUE', value: '20150719T100000' }), | ||
new Property({ name: 'SUMMARY', value: 'To Do (the purpose of creating this long string is to test the 75 character limit per the RFC)' }), | ||
new Property({ | ||
name: 'SUMMARY', | ||
value: 'To Do (the purpose of creating this long string is to test the 75 character limit per the RFC)' }), | ||
new Property({ | ||
name: 'DESCRIPTION', | ||
value: 'Test character escaping: \n , ; \\' | ||
}), | ||
new Property({ name: 'CATEGORIES', value: ['WORK', 'FAMILY'] }) | ||
@@ -64,0 +70,0 @@ ] |
@@ -58,2 +58,6 @@ /* eslint-env mocha */ | ||
}), | ||
new Property({ | ||
name: 'DESCRIPTION', | ||
value: 'Test character escaping: \n , ; \\' | ||
}), | ||
new Property({ name: 'CATEGORIES', value: ['WORK', 'FAMILY'] }) | ||
@@ -60,0 +64,0 @@ ] |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
19295
2
380
190
1
- Removedleft-pad@^1.1.0
- Removedleft-pad@1.3.0(transitive)