【问题标题】:React Native: How to parse an ics / ical fileReact Native:如何解析 ics / ical 文件
【发布时间】:2018-03-19 23:20:07
【问题描述】:

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

【问题讨论】:

  • 解析文件的预期结果是什么?
  • 我想要一个包含事件标题和日期/时间数组的事件数组
  • 您能否在 Question 中包含您尝试解决查询的代码?
  • 好吧,现在我正在手动循环它,我只是期待一个已经解决这个问题的好库。
  • 如果您开发了提供预期结果的代码,问题是什么?为什么需要图书馆?

标签: javascript node.js react-native


【解决方案1】:

任何在 2019+ 年寻找这个问题的答案的人,我已经写了一个 npm 模块来解决这个问题!你可以找到它here! (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

解析愉快!

【讨论】:

    【解决方案2】:

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

    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);
            }
          }
        };
    

    【讨论】:

    • 就像看到这个的人的注释一样,这不支持重复事件,这正是您应该使用库而不是重新发明轮子的原因。
    猜你喜欢
    • 1970-01-01
    • 2017-09-20
    • 2016-11-15
    • 2011-07-03
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    相关资源
    最近更新 更多