Class: DiaryBase

(abstract) DiaryBase(file, serialiseropt)

Base class for sleep diary engines

Constructor

(abstract) new DiaryBase(file, serialiseropt)

Parameters:
Name Type Attributes Description
file string | Object object containing the file
serialiser function <optional>
function to serialise output
Source:

Members

serialiser :function

Serialise a value for output
Type:
  • function
Source:

Methods

(static) date(date, timezoneopt)

Create a DateTime object with timezone support
Parameters:
Name Type Attributes Description
date number | string the date to parse
timezone string <optional>
timezone (e.g. "Europe/London")
Source:
Example
let date = DiaryBase.date(123456789,"Etc/GMT");

(static) file_format()

Name of the current file format
Source:

(static) next_dst_change(date, timezoneopt) → {number}

Get the time of the next DST change after the specified date
Parameters:
Name Type Attributes Description
date number | string the date to parse
timezone string <optional>
timezone (e.g. "Europe/London")
Source:
Returns:
- tranisition time, or Infinity if no transitions are expected
Type
number

(static) parse_timestamp(value, epoch_offsetopt) → {number}

parse a timestamp in various formats
Parameters:
Name Type Attributes Description
value Object | number value to analyse
epoch_offset number <optional>
milliseconds between the unix epoch and the native offset
Source:
Returns:
Unix timestamp in milliseconds, or NaN if not parseable
Type
number
Example
let xml = DiaryBase.parse_xml("<foo>");

(protected, static) parse_xml(string)

parse an XML string to a DOM
Parameters:
Name Type Description
string string XML string to parse
Source:
Example
let xml = DiaryBase.parse_xml("<foo>");

(static) register(constructor)

Register a new engine
Parameters:
Name Type Description
constructor function sleep diary engine
Source:
Example
DiaryBase.register(MyClass);

(protected, static) status_matches()

Array of status strings and associated regular expressions
Source:

(static) tc()

Get the main object for managing timezones This object may change in future - prefer date() whenever possible.
Source:

(protected, static) unique(list1, list2, unique_id) → {Array}

return values that exist in the second argument but not the first
Parameters:
Name Type Description
list1 Array first list of values
list2 Array second list of values
unique_id Array | function function that returns the unique ID for a list item
Source:
Returns:
Type
Array
Example
let filtered = DiaryBase.unique(["a","b"],["b","c"],l=>l);
  -> ["c"]

(static) zero_pad(n, lengthopt)

Convert a string to a number with leading zeros
Parameters:
Name Type Attributes Default Description
n number number to pad
length number <optional>
2 length of the output string
Source:
Example
DiaryBase.zero_pad( 1    ) // ->   "01"
DiaryBase.zero_pad( 1, 4 ) // -> "0001"

clone()

Create a deep copy of the current object
Source:

(protected) corrupt(file, message)

Indicates the file is a corrupt file in the specified format
Parameters:
Name Type Description
file string | Object file contents, or filename/contents pairs (for archive files)
message string optional error message
Source:

(protected) invalid(file)

Indicates the file is not valid in our file format
Parameters:
Name Type Description
file string | Object file contents, or filename/contents pairs (for archive files)
Source:

merge(other)

Merge another diary into this one
Parameters:
Name Type Description
other DiaryBase diary to merge in
Source:
Example
diary.merge(my_data);

(protected) serialise()

Serialise data for output
Source:

software_version() → {string}

Version ID for this package
Source:
Returns:
Type
string

timezones() → {Array.<string>}

Complete list of allowed timezones
Source:
Returns:
Type
Array.<string>

to(to_format) → {*}

Convert a value to some other format

Supported formats:

  • url - contents serialised for inclusion in a URL
  • json - contents serialised to JSON
  • storage-line - contents serialised for inclusion in a newline-separated list of diaries
  • Standard - Standard format
  • (other formats) - the name of any other diary format

to_async() supports more formats and should be used where possible. You should only call this function directly if you want to guarantee synchronous execution.

Parameters:
Name Type Description
to_format string requested format
Source:
Returns:
diary data in new format
Type
*
Example
console.log( diary.to("NewFormat") );

to_async(to_format) → {Promise|Object}

Convert a value to some other format

Supported formats:

  • output - contents serialised for output (e.g. to a file)
  • spreadsheet - binary data that can be loaded by a spreadsheet program
  • (formats supported by to())

See also to(), a lower-level function that supports formats that can be generated synchronously. You can use that function if a Promise interface would be cumbersome or unnecessary in a given piece of code.

Parameters:
Name Type Description
to_format string requested format
Source:
Returns:
Promise that returns the converted diary
Type
Promise | Object
Example
diary.to_async("NewFormat").then( reformatted => console.log( reformatted_diary ) );