烧饼论坛 LZ.SB
登录

😡 ChatGPT 你看看你写的代码

431
  • 纳西妲
    UID 3952Lv.6 经常露面博主nahida.im

    有你这样格式化代码的?生气

    const
      POSTHOG_TOKEN
        = 
      "__token";
    
    const
      POSTHOG_ENDPOINT
        = 
      "__url";
    
    const
      compact
        = 
      (
        data
      ) =>
        Object
          .fromEntries(
            Object
              .entries(
                data
              )
              .filter(
              (
                [
                  ,
                  value
                ]
              ) =>
                value
                  !==
                undefined
                  &&
                value
                  !==
                null
                  &&
                value
                  !==
                "",
            ),
        );
    
    export default {
      async
        fetch(
          request
        ) {
        if (
          request
            .method
              !==
          "POST"
        ) {
          return
            new Response(
              "Method Not Allowed",
              {
                status:
                  405,
              }
            );
        }
        const
          payload =
            await
          request
            .json();
        payload
          .api_key
            = 
          POSTHOG_TOKEN;
        payload
          .properties
            ??=
          {};
        const
          cf
            = 
          request.cf ?? {};
        const
          ip
            = 
        request
          .headers
          .get(
            "CF-Connecting-IP"
          );
        const
          userAgent
            = 
        request
          .headers
          .get(
            "User-Agent"
          );
        Object.assign(
          payload.properties,
          compact({
            $ip:
              ip,
            $user_agent:
              userAgent,
            $raw_user_agent:
              userAgent,
            $geoip_country_code:
              cf
                .country,
            $geoip_city_name:
              cf
                .city,
            $geoip_continent_code:
              cf
                .continent,
            $geoip_subdivision_1_name:
              cf
                .region,
            $geoip_subdivision_1_code:
              cf
                .regionCode,
            $geoip_time_zone:
              cf
                .timezone,
            $geoip_latitude:
              cf
                .latitude != null
                  ?
                    Number(
                      cf
                        .latitude
                    )
                  :
                    undefined,
            $geoip_longitude:
              cf
                .longitude != null
                  ?
                    Number(
                      cf
                        .longitude
                    )
                  :
                    undefined,
            $geoip_postal_code:
              cf
                .postalCode,
          }),
        );
    
        return fetch(
          POSTHOG_ENDPOINT,
          {
            method:
              "POST",
            headers:
              {
                "Content-Type":
                  "application/json",
                "User-Agent":
                  userAgent
                    ||
                  "",
              },
            body:
              JSON
                .stringify(
                  payload
                ),
          }
        );
      },
    };
    
  • 收藏支持反对打赏

1 条回复

  • VIP666
    UID 1435Lv.2 新手上路
    #1

    这代码一看就是 AI 写的,格式全乱套了。说实话这种风格丢进 prettier 直接全重排,const 和等号各占一行我是真没见过,看得人头疼。

发表回复