sodiray / radash

Functional utility library - modern, simple, typed, powerful
https://radash-docs.vercel.app
MIT License
4.31k stars 173 forks source link

feat(array):added chunk array #233

Closed Mostafa-Mohammadi closed 1 year ago

Mostafa-Mohammadi commented 1 year ago

Description

Added an array of elements that called chunk and split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.

chunk(['a', 'b', 'c', 'd'], 2) => [['a', 'b'], ['c', 'd']]

Checklist

vercel[bot] commented 1 year ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
radash-docs ✅ Ready (Inspect) Visit Preview Jan 12, 2023 at 9:06AM (UTC)
Mostafa-Mohammadi commented 1 year ago

@rayepps Do you know why testes are failing? even while all the the tests get passed ... same for for verify-cdn

sodiray commented 1 year ago

Hey @Mostafa-Mohammadi 👋 the test failed because the coverage dropped below 100%

Jest: "global" coverage threshold for branches (100%) not met: 99.31%

For the cdn check, I'm not sure. If you change a single digit/space/character in the source after you run the build the check will fail. It's super easy to break in that way. Maybe try running the build and see if there are any local changes to the cdn directory.

One last thing, I love this PR 😍 but I'm pretty sure we already have this functionality in the cluster function.

Mostafa-Mohammadi commented 1 year ago

Hey @Mostafa-Mohammadi 👋 the test failed because the coverage dropped below 100%

Jest: "global" coverage threshold for branches (100%) not met: 99.31%

For the cdn check, I'm not sure. If you change a single digit/space/character in the source after you run the build the check will fail. It's super easy to break in that way. Maybe try running the build and see if there are any local changes to the cdn directory.

One last thing, I love this PR 😍 but I'm pretty sure we already have this functionality in the cluster function.

Thank you so much,

Do you have any idea to improving the package? I want to add some functions like deep flat for nested array by recursive function... What do you think about that ?

sodiray commented 1 year ago

Most the time if I want to add a function I just do so I don't have much in the backlog I want to add but haven't. There is one function I want to add but I can't find a good way because it would require a dependency. I have to hash things a lot, not for security but for identity. I copy this function around a lot

import crypto from 'crypto'
import { isObject } from 'radash'

/**
 * Given a value, returns a hashed string
 * that can be used as an identifier
 */
export const hash = <TValue extends string | object>(value: TValue) => {
  return crypto
    .createHash('md5')
    .update(isObject(value) ? JSON.stringify(value) : value)
    .digest('hex')
}

You can implement a similar function using uuid v5

import { crush, mapValues } from 'radash'
import * as uuid from 'uuid'

const hash = <TObject extends object>(obj: TObject) =>
  uuid.v5(
    JSON.stringify(
      mapValues(crush(obj), (value: any) => {
        if (value === null) return '__null__'
        if (value === undefined) return '__undefined__'
        return value
      })
    ),
    uuid.v5.DNS
  )

Anyway, this is the one thing on my wish list I haven't added.

I think adding recursion to the flat function would be a great improvement!. There's also #45 to create a pipe function. Thats something I think could be useful but probably only if we also did #224. I've been thinking of adding an all function to the async module. It would act like parallel but expose an interface more similar to Promise.all so instead of mapping over an array of items it would take in a list of promise-returning functions.

I guess I have more ideas than I thought :) I'd be stoked for you to pursue what your passionate about, I'll help however I can.

Mostafa-Mohammadi commented 1 year ago

Most the time if I want to add a function I just do so I don't have much in the backlog I want to add but haven't. There is one function I want to add but I can't find a good way because it would require a dependency. I have to hash things a lot, not for security but for identity. I copy this function around a lot

import crypto from 'crypto'
import { isObject } from 'radash'

/**
 * Given a value, returns a hashed string
 * that can be used as an identifier
 */
export const hash = <TValue extends string | object>(value: TValue) => {
  return crypto
    .createHash('md5')
    .update(isObject(value) ? JSON.stringify(value) : value)
    .digest('hex')
}

You can implement a similar function using uuid v5

import { crush, mapValues } from 'radash'
import * as uuid from 'uuid'

const hash = <TObject extends object>(obj: TObject) =>
  uuid.v5(
    JSON.stringify(
      mapValues(crush(obj), (value: any) => {
        if (value === null) return '__null__'
        if (value === undefined) return '__undefined__'
        return value
      })
    ),
    uuid.v5.DNS
  )

Anyway, this is the one thing on my wish list I haven't added.

I think adding recursion to the flat function would be a great improvement!. There's also #45 to create a pipe function. Thats something I think could be useful but probably only if we also did #224. I've been thinking of adding an all function to the async module. It would act like parallel but expose an interface more similar to Promise.all so instead of mapping over an array of items it would take in a list of promise-returning functions.

I guess I have more ideas than I thought :) I'd be stoked for you to pursue what your passionate about, I'll help however I can.

Well,

I think for implementing hash function we could use Web Crypto API without any third party .

const value = 'hello';

// Create a hash from the value using the Crypto API
const hash = Crypto.createHash('sha256');
hash.update(value);
const hashedString = hash.digest('hex');

console.log(hashedString);  // Output: "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"

The good part of this Web API is it support almost all browsers and versions...

sodiray commented 1 year ago

Oh wow I didn't even know about that. I'm not sure how we would do the import, because we'd need to import crypto for Node and just reference the global Crypto in browser. I'm sure it's possible, I just don't know off the top of my head.

sodiray commented 1 year ago

Hey @Mostafa-Mohammadi I'm gonna close this since the array chunk function isn't going to happen but I'm stoked to see another PR for something else. Feel free to create an issue if you want to discuss more 👍