New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

daify

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

daify

dictify and arrayify

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

daify

dictify and arrayify

API

opts

Object

opts.by
string: specify the path to get key of a item, example: `id`, `n.id`
function: specify the function to get key of a item, example: item => item.id
opts.include[path]
opts for inner object
path can have dot like opts.by, which specifies the path to a inner array or dict.

string

shortcut for opts.by

dictify(array, opts)

Make array into dict

const opts = {
  by: "id",
  include: {
    lists: "id",
    "namespace.inners": "id"
  }
};

const array = [
  { id: 1, lists: [{ id: 1 }] },
  { id: 2, lists: [{ id: 1 }] },
  {
    id: 3,
    lists: [{ id: 1 }],
    namespace: {
      inners: [{ id: 1 }, { id: 2 }, { id: 3 }]
    }
  }
];

log(dictify(array, opts));
/*
{
  "1": {
    "id": 1,
    "lists": {
      "1": {
        "id": 1
      }
    }
  },
  "2": {
    "id": 2,
    "lists": {
      "1": {
        "id": 1
      }
    }
  },
  "3": {
    "id": 3,
    "lists": {
      "1": {
        "id": 1
      }
    },
    "namespace": {
      "inners": {
        "1": {
          "id": 1
        },
        "2": {
          "id": 2
        },
        "3": {
          "id": 3
        }
      }
    }
  }
}
*/

arrayify(dict, opts)

Make dict into array

const opts = {
  by: "id", // by is useless here
  include: {
    lists: "id",
    "namespace.inners": "id"
  }
};
const dict = {
  "1": {
    id: 1,
    lists: {
      "1": { id: 1 }
    }
  },
  "2": {
    id: 2,
    lists: {
      "1": { id: 1 }
    }
  },
  "3": {
    id: 3,
    lists: { "1": { id: 1 } },
    namespace: {
      inners: {
        "1": { id: 1 },
        "2": { id: 2 },
        "3": { id: 3 }
      }
    }
  }
};

log(arrayify(dict, opts));
/*
[
  {
    "id": 1,
    "lists": [
      {
        "id": 1
      }
    ]
  },
  {
    "id": 2,
    "lists": [
      {
        "id": 1
      }
    ]
  },
  {
    "id": 3,
    "lists": [
      {
        "id": 1
      }
    ],
    "namespace": {
      "inners": [
        {
          "id": 1
        },
        {
          "id": 2
        },
        {
          "id": 3
        }
      ]
    }
  }
]
*/

LICENSE

MIT

Keywords

dict

FAQs

Package last updated on 19 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts