Socket
Socket
Sign inDemoInstall

@oada/oadaify

Package Overview
Dependencies
Maintainers
8
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@oada/oadaify - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

test/tsconfig.json

30

lib/index.d.ts

@@ -1,3 +0,11 @@

import type { Except, Mutable } from 'type-fest';
/**
* @license
* Copyright 2022 Alex Layton
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
import type { Except } from 'type-fest';
/**
* Symbol to access OADA `_id` key

@@ -19,5 +27,5 @@ */

/**
* @todo just declare symbols in here is TS stops being dumb about symbol keys
* @todo just declare symbols in here if TS stops being dumb about symbol keys
*/
export declare const Symbols: {
declare const Symbols: {
readonly _id: typeof _id;

@@ -31,3 +39,3 @@ readonly _rev: typeof _rev;

};
export declare type JsonArray = readonly JsonValue[];
export declare type JsonArray = JsonValue[] | readonly JsonValue[];
export declare type JsonValue = string | number | boolean | null | JsonObject | JsonArray;

@@ -39,9 +47,9 @@ declare type OADAified<T> = T extends JsonValue ? OADAifiedJsonValue<T> : never;

export declare type OADAifiedJsonObject<T extends JsonObject = JsonObject> = {
[_id]: OADAified<T['_id']>;
[_rev]: OADAified<T['_rev']>;
[_type]: OADAified<T['_type']>;
[_id]?: OADAified<T['_id']>;
[_rev]?: OADAified<T['_rev']>;
[_type]?: OADAified<T['_type']>;
/**
* @todo OADAify under _meta or not?
*/
[_meta]: OADAified<T['_meta']>;
[_meta]?: OADAified<T['_meta']>;
} & {

@@ -53,3 +61,3 @@ [K in keyof Except<T, keyof typeof Symbols>]: OADAified<T[K]>;

*/
export declare type OADAifiedJsonArray<T extends JsonArray = JsonArray> = readonly OADAifiedJsonValue<T extends readonly (infer R)[] ? R : never>[];
export declare type OADAifiedJsonArray<T extends JsonArray = JsonArray> = Array<OADAifiedJsonValue<T extends Array<infer R> ? R : T extends ReadonlyArray<infer R> ? R : never>>;
/**

@@ -69,3 +77,3 @@ * @todo Better name

*/
export declare function oadaify<T extends JsonValue>(value: T): Mutable<OADAifiedJsonValue<T>>;
export declare function oadaify<T extends Readonly<JsonValue>>(value: T, deep?: boolean): OADAifiedJsonValue<T>;
/**

@@ -78,4 +86,4 @@ * Inverse of oadaify

*/
export declare function deoadaify<T extends JsonValue>(value: OADAifiedJsonValue<T>): Mutable<T>;
export declare function deoadaify<T extends JsonValue>(value: OADAifiedJsonValue<T>): T;
export default oadaify;
//# sourceMappingURL=index.d.ts.map
"use strict";
/**
* @license
* Copyright 2022 Alex Layton
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.deoadaify = exports.oadaify = exports.Symbols = exports._meta = exports._type = exports._rev = exports._id = void 0;
exports.deoadaify = exports.oadaify = exports._meta = exports._type = exports._rev = exports._id = void 0;
// TS is dumb about symbol keys

@@ -22,5 +30,6 @@ /**

/**
* @todo just declare symbols in here is TS stops being dumb about symbol keys
* @todo just declare symbols in here if TS stops being dumb about symbol keys
*/
exports.Symbols = {
// eslint-disable-next-line @typescript-eslint/naming-convention
const Symbols = {
_id: exports._id,

@@ -31,3 +40,6 @@ _rev: exports._rev,

};
function oadaify(value) {
function isArray(value) {
return Array.isArray(value);
}
function oadaify(value, deep = true) {
if (!value || typeof value !== 'object') {

@@ -37,39 +49,45 @@ // Nothing to OADAify

}
if (Array.isArray(value)) {
// Map outself over arrays
return value.map((val) => {
const out = oadaify(val);
return out;
});
if (isArray(value)) {
// Map ourself over arrays
return deep
? value.map((v) => oadaify(v))
: Array.from(value);
}
// TODO: Why is TS being dumb and thinking it can be an array here?
const obj = value;
const out = {};
for (const key in obj) {
// Recurse
out[key] = oadaify(obj[key]);
}
// Preserve symbols
for (const sym of Object.getOwnPropertySymbols(obj)) {
// TS is a jerk about symbol indexing. This line is prefectly vaild.
// @ts-ignore
out[sym] = obj[sym];
}
const out = deep
? Object.fromEntries(Object.entries(value).map(([k, v]) => [k, oadaify(v)]))
: { ...value };
// OADAify any OADA keys
// Have to explicitly handle each symbol for TS to understand...
if (out.hasOwnProperty('_id')) {
out[exports._id] = out._id + '';
Object.defineProperty(out, '_id', { enumerable: false });
if ('_id' in value) {
// eslint-disable-next-line security/detect-object-injection
out[exports._id] = `${value._id}`;
Object.defineProperty(out, '_id', {
value: value._id,
enumerable: false,
});
}
if (out.hasOwnProperty('_rev')) {
out[exports._rev] = +out._rev;
Object.defineProperty(out, '_rev', { enumerable: false });
if ('_rev' in value) {
// eslint-disable-next-line security/detect-object-injection
out[exports._rev] = Number(value._rev);
Object.defineProperty(out, '_rev', {
value: value._rev,
enumerable: false,
});
}
if (out.hasOwnProperty('_type')) {
out[exports._type] = out._type + '';
Object.defineProperty(out, '_type', { enumerable: false });
if ('_type' in value) {
// eslint-disable-next-line security/detect-object-injection
out[exports._type] = `${value._type}`;
Object.defineProperty(out, '_type', {
value: value._type,
enumerable: false,
});
}
if (out.hasOwnProperty('_meta')) {
out[exports._meta] = out._meta;
Object.defineProperty(out, '_meta', { enumerable: false });
// TODO: Should _meta be OADAified?
if ('_meta' in value) {
// eslint-disable-next-line security/detect-object-injection
out[exports._meta] = value._meta;
Object.defineProperty(out, '_meta', {
value: value._meta,
enumerable: false,
});
}

@@ -81,2 +99,9 @@ // Make the JSON still right

exports.oadaify = oadaify;
/**
* Inverse of oadaify
*
* Makes OADA keys normal object properties again.
*
* @see oadaify
*/
function deoadaify(value) {

@@ -87,22 +112,21 @@ if (!value || typeof value !== 'object') {

if (Array.isArray(value)) {
return value.map(deoadaify);
return value.map((v) => deoadaify(v));
}
const out = {};
// TODO: Why is TS being dumb and thinking it can be an array here?
const obj = value;
for (const [key, val] of Object.entries(obj)) {
out[key] = deoadaify(val);
}
const out = Object.fromEntries(Object.entries(value).map(([k, v]) => [k, deoadaify(v)]));
// Add OADA keys
if (obj.hasOwnProperty(exports._id)) {
out._id = obj[exports._id];
if (Object.prototype.hasOwnProperty.call(value, exports._id)) {
// eslint-disable-next-line security/detect-object-injection
out._id = value[exports._id];
}
if (obj.hasOwnProperty(exports._rev)) {
out._rev = obj[exports._rev];
if (Object.prototype.hasOwnProperty.call(value, exports._rev)) {
// eslint-disable-next-line security/detect-object-injection
out._rev = value[exports._rev];
}
if (obj.hasOwnProperty(exports._type)) {
out._type = obj[exports._type];
if (Object.prototype.hasOwnProperty.call(value, exports._type)) {
// eslint-disable-next-line security/detect-object-injection
out._type = value[exports._type];
}
if (obj.hasOwnProperty(exports._meta)) {
out._meta = obj[exports._meta];
if (Object.prototype.hasOwnProperty.call(value, exports._meta)) {
// eslint-disable-next-line security/detect-object-injection
out._meta = value[exports._meta];
}

@@ -109,0 +133,0 @@ return out;

@@ -0,2 +1,18 @@

/**
* @license
* Copyright 2022 Qlever LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export {};
//# sourceMappingURL=index.spec.d.ts.map
// # sourceMappingURL=index.spec.d.ts.map

@@ -0,2 +1,18 @@

/**
* @license
* Copyright 2022 Qlever LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export {};
//# sourceMappingURL=arrays.spec.d.ts.map
// # sourceMappingURL=arrays.spec.d.ts.map

@@ -7,2 +7,2 @@ /**

export {};
//# sourceMappingURL=compatability.spec.d.ts.map
// # sourceMappingURL=compatability.spec.d.ts.map

@@ -0,2 +1,18 @@

/**
* @license
* Copyright 2022 Qlever LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export {};
//# sourceMappingURL=index.spec.d.ts.map
// # sourceMappingURL=index.spec.d.ts.map

@@ -7,2 +7,2 @@ /**

export {};
//# sourceMappingURL=oadaKeys.spec.d.ts.map
// # sourceMappingURL=oadaKeys.spec.d.ts.map

@@ -0,2 +1,18 @@

/**
* @license
* Copyright 2022 Qlever LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export {};
//# sourceMappingURL=primitives.spec.d.ts.map
// # sourceMappingURL=primitives.spec.d.ts.map
{
"name": "@oada/oadaify",
"version": "1.0.1",
"version": "2.0.0",
"description": "Make OADA data nicer to work with in JS/TS",

@@ -15,3 +15,2 @@ "keywords": [

"license": "MIT",
"private": false,
"engines": {

@@ -31,28 +30,65 @@ "node": ">=12"

"build": "tsc -b",
"clean": "tsc -b --clean",
"pretest": "npm run build",
"test": "ava",
"clean": "yarn run build --clean",
"lint": "eslint .",
"test": "yarn run build test && ava",
"prepare": "npm run build"
},
"ava": {
"files": [
"**/*.spec.ts"
],
"cache": false,
"typescript": {
"extensions": [
"ts"
],
"rewritePaths": {
"src/": "lib/"
}
"src/": "lib/",
"test/": ".test/"
},
"compile": false
}
},
"devDependencies": {
"@ava/typescript": "^1.1.1",
"@tsconfig/node12": "^1.0.7",
"@types/node": "^14.14.22",
"ajv": "^7.0.3",
"ava": "^3.15.0",
"@ava/typescript": "^3.0.1",
"@tsconfig/node12": "^1.0.9",
"@types/node": "^14.18.10",
"@typescript-eslint/eslint-plugin": "^5.10.2",
"@typescript-eslint/parser": "^5.10.2",
"@yarnpkg/sdks": "^2.6.0-rc.6",
"ajv": "^8.10.0",
"ava": "4.0.0-rc.1",
"eslint": "^8.8.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-xo": "^0.39.0",
"eslint-config-xo-typescript": "^0.49.0",
"eslint-formatter-pretty": "^4.1.0",
"eslint-import-resolver-node": "^0.3.6",
"eslint-plugin-array-func": "^3.1.7",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-filenames": "^1.3.2",
"eslint-plugin-github": "^4.3.5",
"eslint-plugin-i18n-text": "^1.0.1",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-no-constructor-bind": "^2.0.4",
"eslint-plugin-no-only-tests": "^2.6.0",
"eslint-plugin-no-secrets": "^0.8.9",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-notice": "^0.9.10",
"eslint-plugin-optimize-regex": "^1.2.1",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-regexp": "^1.5.1",
"eslint-plugin-security": "^1.4.0",
"eslint-plugin-sonarjs": "^0.11.0",
"eslint-plugin-unicorn": "^40.1.0",
"jsonschema8": "^1.1.1",
"prettier": "^2.2.1",
"ts-expect": "^1.1.0",
"typescript": "^4.1.3"
"prettier": "^2.5.1",
"ts-expect": "^1.3.0",
"typescript": "^4.5.5"
},
"dependencies": {
"type-fest": "^0.20.2"
}
}
"type-fest": "^2.11.2"
},
"packageManager": "yarn@3.1.1"
}

@@ -7,3 +7,3 @@ # @OADA/oadaify

or forgetting to add the check and my code did something weird.
This makes loops etc. do what one expecst and wants 99% of the time.
This makes loops etc. do what one expects and wants 99% of the time.
The library exports `Symbol`s which can be used to access the OADA keys.

@@ -10,0 +10,0 @@

@@ -0,3 +1,12 @@

/**
* @license
* Copyright 2022 Alex Layton
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
// This is better than Omit
import type { Except, Mutable } from 'type-fest';
import type { Except } from 'type-fest';

@@ -23,5 +32,6 @@ // TS is dumb about symbol keys

/**
* @todo just declare symbols in here is TS stops being dumb about symbol keys
* @todo just declare symbols in here if TS stops being dumb about symbol keys
*/
export const Symbols = <const>{
// eslint-disable-next-line @typescript-eslint/naming-convention
const Symbols = <const>{
_id,

@@ -35,3 +45,3 @@ _rev,

export type JsonObject = { [Key in string]?: JsonValue };
export type JsonArray = readonly JsonValue[];
export type JsonArray = JsonValue[] | readonly JsonValue[];
export type JsonValue =

@@ -41,2 +51,3 @@ | string

| boolean
// eslint-disable-next-line @typescript-eslint/ban-types
| null

@@ -52,9 +63,9 @@ | JsonObject

export type OADAifiedJsonObject<T extends JsonObject = JsonObject> = {
[_id]: OADAified<T['_id']>;
[_rev]: OADAified<T['_rev']>;
[_type]: OADAified<T['_type']>;
[_id]?: OADAified<T['_id']>;
[_rev]?: OADAified<T['_rev']>;
[_type]?: OADAified<T['_type']>;
/**
* @todo OADAify under _meta or not?
*/
[_meta]: OADAified<T['_meta']>;
[_meta]?: OADAified<T['_meta']>;
} & {

@@ -67,5 +78,7 @@ [K in keyof Except<T, keyof typeof Symbols>]: OADAified<T[K]>;

*/
export type OADAifiedJsonArray<
T extends JsonArray = JsonArray
> = readonly OADAifiedJsonValue<T extends readonly (infer R)[] ? R : never>[];
export type OADAifiedJsonArray<T extends JsonArray = JsonArray> = Array<
OADAifiedJsonValue<
T extends Array<infer R> ? R : T extends ReadonlyArray<infer R> ? R : never
>
>;

@@ -75,9 +88,8 @@ /**

*/
export type OADAifiedJsonValue<
T extends JsonValue = JsonValue
> = T extends JsonArray
? OADAifiedJsonArray<T>
: T extends JsonObject
? OADAifiedJsonObject<T>
: T;
export type OADAifiedJsonValue<T extends JsonValue = JsonValue> =
T extends JsonArray
? OADAifiedJsonArray<T>
: T extends JsonObject
? OADAifiedJsonObject<T>
: T;

@@ -89,2 +101,8 @@ /**

function isArray<T>(
value: T | T[] | readonly T[]
): value is T[] | readonly T[] {
return Array.isArray(value);
}
/**

@@ -96,8 +114,7 @@ * Converts OADA keys (i.e., ones starting with `_`) to Symbols

*/
export function oadaify<T extends JsonValue>(
value: T
): Mutable<OADAifiedJsonValue<T>>;
export function oadaify(
value: Readonly<JsonValue>
): Mutable<OADAifiedJsonValue<JsonValue>> {
export function oadaify<T extends Readonly<JsonValue>>(
value: T,
deep?: boolean
): OADAifiedJsonValue<T>;
export function oadaify(value: JsonValue, deep = true): OADAifiedJsonValue {
if (!value || typeof value !== 'object') {

@@ -108,41 +125,52 @@ // Nothing to OADAify

if (Array.isArray(value)) {
// Map outself over arrays
return (value as JsonArray).map((val) => {
const out = oadaify(val);
return out;
});
if (isArray(value)) {
// Map ourself over arrays
return deep
? value.map((v) => oadaify(v)!)
: (Array.from(value) as OADAifiedJsonArray);
}
// TODO: Why is TS being dumb and thinking it can be an array here?
const obj = value as JsonObject;
const out = {} as OADAifiedJsonObject<JsonObject>;
for (const key in obj) {
// Recurse
out[key] = oadaify(obj[key]!);
}
// Preserve symbols
for (const sym of Object.getOwnPropertySymbols(obj)) {
// TS is a jerk about symbol indexing. This line is prefectly vaild.
// @ts-ignore
out[sym] = obj[sym];
}
const out: OADAifiedJsonObject = deep
? Object.fromEntries(
Object.entries(value).map(([k, v]) => [k, oadaify(v!)!])
)
: ({ ...value } as unknown as OADAifiedJsonObject);
// OADAify any OADA keys
// Have to explicitly handle each symbol for TS to understand...
if (out.hasOwnProperty('_id')) {
out[_id] = out._id + '';
Object.defineProperty(out, '_id', { enumerable: false });
if ('_id' in value) {
// eslint-disable-next-line security/detect-object-injection
out[_id] = `${value._id}`;
Object.defineProperty(out, '_id', {
value: value._id,
enumerable: false,
});
}
if (out.hasOwnProperty('_rev')) {
out[_rev] = +out._rev!;
Object.defineProperty(out, '_rev', { enumerable: false });
if ('_rev' in value) {
// eslint-disable-next-line security/detect-object-injection
out[_rev] = Number(value._rev);
Object.defineProperty(out, '_rev', {
value: value._rev,
enumerable: false,
});
}
if (out.hasOwnProperty('_type')) {
out[_type] = out._type + '';
Object.defineProperty(out, '_type', { enumerable: false });
if ('_type' in value) {
// eslint-disable-next-line security/detect-object-injection
out[_type] = `${value._type}`;
Object.defineProperty(out, '_type', {
value: value._type,
enumerable: false,
});
}
if (out.hasOwnProperty('_meta')) {
out[_meta] = out._meta as OADAified<JsonObject>;
Object.defineProperty(out, '_meta', { enumerable: false });
// TODO: Should _meta be OADAified?
if ('_meta' in value) {
// eslint-disable-next-line security/detect-object-injection
out[_meta] = value._meta as OADAifiedJsonValue;
Object.defineProperty(out, '_meta', {
value: value._meta,
enumerable: false,
});
}

@@ -165,33 +193,37 @@

value: OADAifiedJsonValue<T>
): Mutable<T>;
export function deoadaify(value: OADAifiedJsonValue): Mutable<JsonValue> {
): T {
if (!value || typeof value !== 'object') {
return value;
return value as T;
}
if (Array.isArray(value)) {
return value.map(deoadaify);
return value.map((v) => deoadaify<JsonValue>(v)) as unknown as T;
}
const out: JsonObject = {};
// TODO: Why is TS being dumb and thinking it can be an array here?
const obj = value as OADAifiedJsonObject<JsonObject>;
for (const [key, val] of Object.entries(obj)) {
out[key] = deoadaify<JsonValue>(val);
}
const out = Object.fromEntries(
Object.entries(value).map(([k, v]) => [k, deoadaify<JsonValue>(v)])
);
// Add OADA keys
if (obj.hasOwnProperty(_id)) {
out._id = obj[_id];
if (Object.prototype.hasOwnProperty.call(value, _id)) {
// eslint-disable-next-line security/detect-object-injection
out._id = value[_id]!;
}
if (obj.hasOwnProperty(_rev)) {
out._rev = obj[_rev];
if (Object.prototype.hasOwnProperty.call(value, _rev)) {
// eslint-disable-next-line security/detect-object-injection
out._rev = value[_rev]!;
}
if (obj.hasOwnProperty(_type)) {
out._type = obj[_type];
if (Object.prototype.hasOwnProperty.call(value, _type)) {
// eslint-disable-next-line security/detect-object-injection
out._type = value[_type]!;
}
if (obj.hasOwnProperty(_meta)) {
out._meta = obj[_meta];
if (Object.prototype.hasOwnProperty.call(value, _meta)) {
// eslint-disable-next-line security/detect-object-injection
out._meta = value[_meta]!;
}
return out;
return out as T;
}

@@ -202,3 +234,3 @@

*/
function toJSON(this: OADAifiedJsonObject<JsonObject>) {
function toJSON(this: OADAifiedJsonObject) {
return deoadaify(this);

@@ -205,0 +237,0 @@ }

{
"extends": "@tsconfig/node12/tsconfig.json",
"extends": "@tsconfig/node12",
"compilerOptions": {
"composite": true,
"rootDir": "src",

@@ -15,3 +16,4 @@ "outDir": "lib",

"stripInternal": true
}
},
"include": ["src"]
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc