Skip Navigation
type-safe GroupBy (key) function i wrote

i made a type-safe GroupBy function. ```typescript /**

  • Groups array of objects by a given key
  • @param arr array of objects to group
  • @param key must be present on every object, and it's values must be string|number
  • @author telepresence
  • @license CC-BY-4.0 */ function groupBy(arr: T[], key: keyof T, defaultAcc: Record = {}) { return arr.reduce((acc, val, i) => { const compValue = val[key]; if (typeof compValue !== 'string' && typeof compValue !== 'number') { throw new Error(key ${key.toString()} has values other than string/number. can only group by string/number values); } if (!acc[compValue]) acc[compValue] = [] acc[compValue].push(val); return acc; }, defaultAcc); } ```
  • like lodash's groupBy, but by key and not function
    • group an array of objects which all have a key in common into an object with keys matching all the different possible values of your common key
  • type-safe, no unknown's no any's
  • does not copy arrays ([...array]), uses push
  • supports selecting by keys, where the key values are string / number (although you can easily add symbol support)
  • shared for free under the CC BY 4.0 license - only attribution is requred (link to this post is fine)
  • custom default accumulator support, if you already know the groups beforehand and would rather have an empty array than undefined.

example: ```typescript const data = [{ "name": "jim", "color": "blue", "age": "22" }, { "name": "Sam", "color": "blue", "age": "33" }, { "name": "eddie", "color": "green", "age": "77" }];

groupBy(data, 'color') would result into:ts { "blue": [ { "name": "jim", "color": "blue", "age": "22" }, { "name": "Sam", "color": "blue", "age": "33" } ], "green": [ { "name": "eddie", "color": "green", "age": "77" } ] } ``` TL;DR i've sucessfully wrote something using generics in typescript for the first time, and i think it's pretty epic.

0
Can you recommend contemporary music artists without lyrics?
  • Polyphia, Casiopea (Mint Jams album)

  • The TypeBoy Keyboard & TypePak Cartridge
  • based based based i love this keyboard and your site too

  • I am writing this nodeJS project, and I wanted to include the typescript mastodon library but it keeps telling me this. I am kinda unsure how to proceed.
  • from the error it looks like you're importing an es module inside a common.js environment. but as @clif@lemmy.world said, there are several things that could cause this.

  • A good place to listen to audio books online?
  • these two tools apparently let you rip borrowed audiobooks from this service named libby. although i haven't tested them. https://github.com/ping/odmpy https://github.com/bookbonobo/libby-download-extension

    also, have you checked the index? stuff like this is usually there.

  • where is an OK place to experiment with bots?
  • good call asking for a proper venue to test this, but how do you mean you can't remove federated stuff? i was under the impression (from lemmy's homepage) that one of the features is 100% complete deletion by replacing post/comment content with 'removed by user'. is this not the case?

  • Microsoft is using malware-like pop-ups in Windows 11 to get people to ditch Google
  • Check out some alternatives:
    searXNG - open source & self-hosted meta-search engine (aggregates results from many others, like google, bing, qwant, duckduckgo - configurable which ones.) list of public instances just pick one that's close to you physically and has a good uptime.
    duckduckgo - uses bing for most search results, but is way more private
    brave search - uses their own index, has a privacy-respecting privacy policy and the results are pretty damn good

  • Koven Wei - EVERYTHING IS RED

    Banger i found randomly when checking the twitter of one of my favorite artists, @xyanaid. They made the album cover.

    edit: Spotify link

    0
    The Printer That Simply Worked
  • fyi, lemmy doesen't have sublemmies, but communities. that's why the url is instance/c/community or yourinstance/c/community@instance

  • has your YouTube consumption decreased after implementing the "no history, no recommendations" feature?
  • spent a few hours yesterday sorting all of my subscriptions and moving them to newpipe. while having everything auto-backed up by google was convenient, newpipe is much better - i go in, watch some videos i saved into a playlist or that have come out from subscribed channels, then go out. it feels way better then being sucked in by the algorithm. i think i'll still use youtube's algorithm on my pc sometimes, as this setup hinders new content discovery. but overall, i'm really pleased so far with the new setup.

  • Here's the plan. (New video from LTT)
  • Yes, if you buy product X tested on whatever YouTube channel, your mileage may vary.
    However, LTT had several graphs where they showed egregious performance increases in newer models of graphics cards compared to older ones. Somebody drawing a conclusion just from the relative performance increase of over 200%, even if the numbers would've been slightly off otherwise, will be more likely to make a bad decision based on the relative (non-existent) insane performance increase.

  • Here's the plan. (New video from LTT)
  • Check out Gamers Nexus. In their last ITX case review, you can see just the level of immense detail, specifications and testing for a simple computer case.

  • Here's the plan. (New video from LTT)
  • I am glad that they plan to improve and made steps towards it, but i still have a sour taste in my mouth:

    • I was expecting a formal apology for the monoblock and mouse skates film issue. Both of these were instances where LTT threw another smaller company under the bus. Them not addressing it further gives the assumption that they can, and will keep getting away with stuff like this.
    • Their new guidelines for correction policy are flawed; even the low-severity ones are thing that really shouldn't be tolerated with no corrective action, and all factual mistakes should be re-shot or voiced over instead of on-screen corrections.
    • They hardly touched upon the whole Madison situation, except for some boasting about employee benefits.
    • In the whole situation, I felt like they failed to really admit that they were sorry for what hapened, and were taking the role of the victims.
      • "We are people too" and a whole segment in this video of emails from fans hoping they will get better soon and "survive this difficult shitstorm". LMG was merely getting away with this stuff for a long time and just now have been called out. They don't deserve any harassment, however, they also don't deserve any "Get better soon" emails from fans.

    TL;DR: I will most likely continue to watch Linus tech tips for entertainment purposes, but will no longer trust them on any technical details, and would go to other channels for tutorials on how to build a pc and such.

  • One Device to Do it All
  • nice read

  • The monkeyboard, custom made with Ergogen
  • looks pretty cool, like a bigger kyria. nice site too!

  • telepresence telepresence @discuss.tchncs.de

    self-induced insomniac. I like yerba maté and coding.

    Posts 2
    Comments 15