React Native:如何解析 ics / ical 文件

Ada*_*amG 5 javascript node.js react-native

解析 ical / ics 文件的最佳方法是什么?我把它全部放在一个字符串中,但我找不到与 RN 兼容的包或库。所有一般的 JS 或 Node 都会抛出关于 FS 包丢失的错误。我正在从 URL 中读取它并将其全部保存在内存中,因此我什至不需要文件系统访问权限。

Chr*_*rdi 8

任何在 2019+ 年寻找这个问题答案的人,我写了一个 npm 模块来解决这个问题!你可以在这里找到它!(https://github.com/Christop406/ical-parser)。

或者,如果您很懒惰,只需运行npm i cal-parserNOT ical-parser)。

你可以这样使用它:

const ical = require('cal-parser');

var parsedCal = ical.parseString(MY_ICAL_STRING);

console.log(parsedCal.calendarData); // for calendar metadata
console.log(parsedCal.events);       // for calendar events
Run Code Online (Sandbox Code Playgroud)

快乐解析!


Ada*_*amG 4

我找不到任何这样的模块。这是我用来解决这个问题的基本代码:

var request = new XMLHttpRequest();
    request.open(
      "GET",
      "https://calendar.google.com/calendar/ical/en.usa%23holiday%40group.v.calendar.google.com/public/basic.ics",
      true
    );
    request.send(null);
    request.onreadystatechange = function() {
      if (request.readyState === 4 && request.status === 200) {
        var type = request.getResponseHeader("Content-Type");
        if (type.indexOf("text") !== 1) {
          var lines = request.responseText.split("\n");
          var events = {}
          var events_i = 0;
          for (i = 0; i < lines.length; i++) {
            if (lines[i].includes('DTSTART')) {
              var date = lines[i].split(":");
              events[events_i] = {date: date[1]};
            }
            else if (lines[i].includes('SUMMARY')) {
              var title = lines[i].split(":");
              events[events_i]["title"] = title[1];
            }
            else if (lines[i].includes('END:VEVENT')) {
              events_i++;
            }
          }
          console.log(events);
        }
      }
    };
Run Code Online (Sandbox Code Playgroud)