从推文(字符串)返回某些数据的函数

发布于 2025-01-22 15:23:38 字数 2152 浏览 0 评论 0原文

我已经写了下面的功能,该功能采用一个参数(字符串)并从中提取某些数据。

function getTweetData(tweet) {


const newObject = {
    tags: [],
    mentions: [],
    tagCount: 0,
    mentionCount: 0,
    length: 0,
  };

  //length
  newObject.length = tweet.length;

  const findMention = tweet.split("@").length - 1;
  newObject.mentionCount = findMention;
  const atRegex = tweet.match(/@(.[a-z0-9]+)/g);
  newObject.mentions = atRegex === null ? [] : atRegex;

  const findHashtag = tweet.split("#").length - 1;
  newObject.tagCount = findHashtag;
  const regex = tweet.match(/#(.[a-z0-9]+)/g);
  newObject.tags = regex === null ? [] : regex;

  //need to put the above two together


  console.log(newObject);
  return newObject;
}

该代码采用一个字符串,并尝试提取字符数量, @的数量和#的数量。然后,应将 @和#内容输入相应的数组(以const newObject)输入。

上面的代码适用于包含 @或#但不包含两者的字符串的字符串。

关于如何结合两者的任何想法。

我写的测试在下面,除了最后一个测试。

    const getTweetData = require("../get-tweet-data");

describe("Name of the group", () => {
  test("should return return empty object", () => {
    expect(typeof getTweetData(" ")).toBe("object");
  });
  test("should return length of character tweets", () => {
    expect(getTweetData("tweet")).toEqual({
      tags: [],
      mentions: [],
      tagCount: 0,
      mentionCount: 0,
      length: 5,
    });
  });
  test("should return amount of hashtags mentioned in tweet", () => {
    expect(getTweetData("tweet #coding")).toEqual({
      tags: ["#coding"],
      mentions: [],
      tagCount: 1,
      mentionCount: 0,
      length: 13,
    });
  });
  test("should return amount of @mentions mentioned in tweet", () => {
    expect(getTweetData("tweet @coders")).toEqual({
      tags: [],
      mentions: ["@coders"],
      tagCount: 0,
      mentionCount: 1,
      length: 18,
    });
  });
  test("should return amount of @mentions and #hashtags mentioned in tweet", () => {
    expect("Hello @stackoverflow #coding #needhelp").toEqual({
      tags: ["#coding", "#needhelp"],
      mentions: ["@stackoverflow"],
      tagCount: 2,
      mentionCount: 1,
      length: 38,
    });
  });
});

I have wrote the function below, which takes a parameter (a string) and extracts certain data out of it.

function getTweetData(tweet) {


const newObject = {
    tags: [],
    mentions: [],
    tagCount: 0,
    mentionCount: 0,
    length: 0,
  };

  //length
  newObject.length = tweet.length;

  const findMention = tweet.split("@").length - 1;
  newObject.mentionCount = findMention;
  const atRegex = tweet.match(/@(.[a-z0-9]+)/g);
  newObject.mentions = atRegex === null ? [] : atRegex;

  const findHashtag = tweet.split("#").length - 1;
  newObject.tagCount = findHashtag;
  const regex = tweet.match(/#(.[a-z0-9]+)/g);
  newObject.tags = regex === null ? [] : regex;

  //need to put the above two together


  console.log(newObject);
  return newObject;
}

The code takes a string and attempts to extract the amount of characters, the number of @ and the number of #. The @ and # contents should then be entered into the respective arrays (in const newObject).

The above code works for strings which contain either @ or # but not for strings which contain both.

Any ideas on how to combine the two.

The tests I have wrote are below, and all except the last one are passing.

    const getTweetData = require("../get-tweet-data");

describe("Name of the group", () => {
  test("should return return empty object", () => {
    expect(typeof getTweetData(" ")).toBe("object");
  });
  test("should return length of character tweets", () => {
    expect(getTweetData("tweet")).toEqual({
      tags: [],
      mentions: [],
      tagCount: 0,
      mentionCount: 0,
      length: 5,
    });
  });
  test("should return amount of hashtags mentioned in tweet", () => {
    expect(getTweetData("tweet #coding")).toEqual({
      tags: ["#coding"],
      mentions: [],
      tagCount: 1,
      mentionCount: 0,
      length: 13,
    });
  });
  test("should return amount of @mentions mentioned in tweet", () => {
    expect(getTweetData("tweet @coders")).toEqual({
      tags: [],
      mentions: ["@coders"],
      tagCount: 0,
      mentionCount: 1,
      length: 18,
    });
  });
  test("should return amount of @mentions and #hashtags mentioned in tweet", () => {
    expect("Hello @stackoverflow #coding #needhelp").toEqual({
      tags: ["#coding", "#needhelp"],
      mentions: ["@stackoverflow"],
      tagCount: 2,
      mentionCount: 1,
      length: 38,
    });
  });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光下慵懒的猫 2025-01-29 15:23:38

您的功能似乎有效。

这是我们在字符串上调用getTweetData时获得的输出:
“ hello @stackoverflow #coding #needhelp”

{
  tags: [ '#coding', '#needhelp' ],
  mentions: [ '@stackoverflow' ],
  tagCount: 2,
  mentionCount: 1,
  length: 38
}

编辑:您上次测试存在错误:您应该致电:

期望(getTweetData(“ hello @stackoverflow #coding #coding #coding #needhelp”))。 to equal({...

而不是期望(“ hello @stackoverflow #coding #needhelp”)。toequal({...

Your function seems to work.

Here is the output we get when we call getTweetData on the string:
"Hello @stackoverflow #coding #needhelp"

{
  tags: [ '#coding', '#needhelp' ],
  mentions: [ '@stackoverflow' ],
  tagCount: 2,
  mentionCount: 1,
  length: 38
}

Edit: There is a mistake in your last test: You should call:

expect(getTweetData("Hello @stackoverflow #coding #needhelp")).toEqual({ ...

instead of expect("Hello @stackoverflow #coding #needhelp").toEqual({ ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文