Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Lightweight, composable mappers for object transformations/normalization.
npm i -S mappet
Cannot read property of undefined
.undefined
valuenull
to empty string to make React inputs happySimple value to value transformation
const schema = {
firstName: "first_name",
cardNumber: "card.number",
};
const mapper = mappet(schema);
const source = {
first_name: "Michal",
last_name: "Zalecki",
card: {
number: "5555-5555-5555-4444",
},
};
const result = mapper(source);
// {
// firstName: "Michal",
// cardNumber: "5555-5555-5555-4444",
// }
Schema entries can be also option objects of path
, modifier
, and include
. Modifier accepts selected value and original source object.
const formatDate = (date, source) => moment(date).format(source.country === "us" ? "MM/DD/YY" : "DD/MM/YY");
const upperCase = v => v.toUpperCase();
const schema = {
country: { path: "country", modifier: upperCase },
date: { path: "date", modifier: formatDate },
};
const mapper = mappet(schema);
const source = {
country: "gb",
date: "2016-07-30",
};
const result = mapper(source);
// {
// country: "GB",
// date: "30/07/16",
// }
Using include
you can control which values should be keept and what dropped.
const isGift = (value, source) => source.isGift;
const schema = {
quantity: ["quantity"],
message: { path: "giftMessage", include: isGift },
remind_before_renewing: { path: "remindBeforeRenewingGift", include: isGift },
};
const mapper = mappet(schema);
const source = {
quantity: 3,
isGift: false,
giftMessage: "All best!",
remindBeforeRenewingGift: true,
};
const result = mapper(source);
// {
// quantity: 3,
// };
Mappers are just closures. It's easy to combine them using modifiers.
const userSchema = {
firstName: "first_name",
lastName: "last_name",
};
const userMapper = mappet(userSchema);
const usersSchema = {
totalCount: "total_count",
users: { path: "items", modifier: users => users.map(userMapper) },
};
const usersMapper = mappet(usersSchema);
const source = {
total_count: 5,
items: [
{ first_name: "Michal", last_name: "Zalecki" },
{ first_name: "John", last_name: "Doe" },
],
};
const result = usersMapper(source);
// {
// totalCount: 5,
// users: [
// { firstName: "Michal", lastName: "Zalecki" },
// { firstName: "John", lastName: "Doe" },
// ],
// }
Mappers in strict mode will throw exception when value is not found on source object.
const schema = {
firstName: "first_name",
lastName: "last_name",
};
const mapper = mappet(schema, { strict: true });
const source = {
first_name: "Michal",
};
const result = mapper(source);
// Uncaught Mappet: last_name not found
You can specify mapper name for easier debugging.
const userMapper = mappet(schema, { strictMode: true, name: "User Mapper" });
const user = mapper(source);
// Uncaught User Mapper: last_name not found
Mappers in greedy mode will copy all properties from source object.
const schema = {
last_name: { path: "last_name", modifier: str => str.toUpperCase() },
};
const mapper = mappet(schema, { greedy: true });
const source = {
first_name: "Michal",
last_name: "Zalecki",
email: "example@michalzalecki.com",
};
const actual = mapper(source);
// {
// first_name: "Michal",
// last_name: "ZALECKI",
// email: "example@michalzalecki.com",
// }
See tests for more examples.
FAQs
Lightweight, composable mappers for object transformations
We found that mappet demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.