Best JavaScript code snippet using ng-mocks
prop.ts
Source:prop.ts
1/** @format */2import * as mongoose from 'mongoose';3import { schema, virtuals, methods } from './data';4import { isPrimitive, initAsObject, initAsArray, isString, isNumber, isObject } from './utils';5import { InvalidPropError, NotNumberTypeError, NotStringTypeError, NoMetadataError } from './errors';6import { ObjectID } from 'bson';7export type Func = (...args: any[]) => any;8export type RequiredType = boolean | [boolean, string] | string | Func | [Func, string];9export type ValidatorFunction = (value: any) => boolean | Promise<boolean>;10export type Validator =11 | ValidatorFunction12 | RegExp13 | {14 validator: ValidatorFunction;15 message?: string;16 };17export interface BasePropOptions {18 required?: RequiredType;19 enum?: string[] | object;20 default?: any;21 validate?: Validator | Validator[];22 unique?: boolean;23 index?: boolean;24 sparse?: boolean;25 expires?: string | number;26 _id?: boolean;27}28export interface PropOptions extends BasePropOptions {29 ref?: any;30}31export interface ValidateNumberOptions {32 min?: number | [number, string];33 max?: number | [number, string];34}35export interface ValidateStringOptions {36 minlength?: number | [number, string];37 maxlength?: number | [number, string];38 match?: RegExp | [RegExp, string];39}40export interface TransformStringOptions {41 lowercase?: boolean; // whether to always call .toLowerCase() on the value42 uppercase?: boolean; // whether to always call .toUpperCase() on the value43 trim?: boolean; // whether to always call .trim() on the value44}45export type PropOptionsWithNumberValidate = PropOptions & ValidateNumberOptions;46export type PropOptionsWithStringValidate = PropOptions & TransformStringOptions & ValidateStringOptions;47export type PropOptionsWithValidate = PropOptionsWithNumberValidate | PropOptionsWithStringValidate;48const isWithStringValidate = (options: PropOptionsWithStringValidate) =>49 options.minlength || options.maxlength || options.match;50const isWithStringTransform = (options: PropOptionsWithStringValidate) =>51 options.lowercase || options.uppercase || options.trim;52const isWithNumberValidate = (options: PropOptionsWithNumberValidate) => options.min || options.max;53const baseProp = (rawOptions: any, Type: any, target: any, key: any, isArray = false) => {54 const name: string = target.constructor.name;55 const isGetterSetter = Object.getOwnPropertyDescriptor(target, key);56 if (isGetterSetter) {57 if (isGetterSetter.get) {58 if (!virtuals[name]) {59 virtuals[name] = {};60 }61 if (!virtuals[name][key]) {62 virtuals[name][key] = {};63 }64 virtuals[name][key] = {65 ...virtuals[name][key],66 get: isGetterSetter.get,67 };68 }69 if (isGetterSetter.set) {70 if (!virtuals[name]) {71 virtuals[name] = {};72 }73 if (!virtuals[name][key]) {74 virtuals[name][key] = {};75 }76 virtuals[name][key] = {77 ...virtuals[name][key],78 set: isGetterSetter.set,79 };80 }81 return;82 }83 if (isArray) {84 initAsArray(name, key);85 } else {86 initAsObject(name, key);87 }88 const ref = rawOptions.ref;89 if (typeof ref === 'string') {90 schema[name][key] = {91 ...schema[name][key],92 type: mongoose.Schema.Types.ObjectId,93 ref,94 };95 return;96 } else if (ref) {97 schema[name][key] = {98 ...schema[name][key],99 type: mongoose.Schema.Types.ObjectId,100 ref: ref.name,101 };102 return;103 }104 const itemsRef = rawOptions.itemsRef;105 if (itemsRef) {106 schema[name][key][0] = {107 ...schema[name][key][0],108 type: mongoose.Schema.Types.ObjectId,109 ref: itemsRef.name,110 };111 return;112 }113 const enumOption = rawOptions.enum;114 if (enumOption) {115 if (!Array.isArray(enumOption)) {116 rawOptions.enum = Object.keys(enumOption).map(propKey => enumOption[propKey]);117 }118 }119 // check for validation inconsistencies120 if (isWithStringValidate(rawOptions) && !isString(Type)) {121 throw new NotStringTypeError(key);122 }123 if (isWithNumberValidate(rawOptions) && !isNumber(Type)) {124 throw new NotNumberTypeError(key);125 }126 // check for transform inconsistencies127 if (isWithStringTransform(rawOptions) && !isString(Type)) {128 throw new NotStringTypeError(key);129 }130 const instance = new Type();131 const subSchema = schema[instance.constructor.name];132 if (!subSchema && !isPrimitive(Type) && !isObject(Type)) {133 throw new InvalidPropError(Type.name, key);134 }135 const { ['ref']: r, ['items']: i, ...options } = rawOptions;136 if (isPrimitive(Type)) {137 if (isArray) {138 schema[name][key] = {139 ...schema[name][key][0],140 ...options,141 type: [Type],142 };143 return;144 }145 schema[name][key] = {146 ...schema[name][key],147 ...options,148 type: Type,149 };150 return;151 }152 // If the 'Type' is not a 'Primitive Type' and no subschema was found treat the type as 'Object'153 // so that mongoose can store it as nested document154 if (isObject(Type) && !subSchema) {155 schema[name][key] = {156 ...schema[name][key],157 ...options,158 type: Object,159 };160 return;161 }162 if (isArray) {163 schema[name][key][0] = {164 ...schema[name][key][0],165 ...options,166 ...subSchema,167 };168 return;169 }170 const Schema = mongoose.Schema;171 const supressSubschemaId = rawOptions._id === false;172 const virtualSchema = new Schema({ ...subSchema }, supressSubschemaId ? { _id: false } : {});173 const schemaInstanceMethods = methods.instanceMethods[instance.constructor.name];174 if (schemaInstanceMethods) {175 virtualSchema.methods = schemaInstanceMethods;176 }177 schema[name][key] = {178 ...schema[name][key],179 ...options,180 type: virtualSchema,181 };182 return;183};184export const prop = (options: PropOptionsWithValidate = {}) => (target: any, key: string) => {185 const Type = (Reflect as any).getMetadata('design:type', target, key);186 if (!Type) {187 throw new NoMetadataError(key);188 }189 baseProp(options, Type, target, key);190};191export interface ArrayPropOptions extends BasePropOptions {192 items?: any;193 itemsRef?: any;194}195export const arrayProp = (options: ArrayPropOptions) => (target: any, key: string) => {196 const Type = options.items;197 baseProp(options, Type, target, key, true);198};...
prop.js
Source:prop.js
1"use strict";2Object.defineProperty(exports, "__esModule", { value: true });3const mongoose = require("mongoose");4const _ = require("lodash");5const data_1 = require("./data");6const utils_1 = require("./utils");7const errors_1 = require("./errors");8const isWithStringValidate = (options) => (options.minlength || options.maxlength || options.match);9const isWithNumberValidate = (options) => (options.min || options.max);10const baseProp = (rawOptions, Type, target, key, isArray = false) => {11 const name = target.constructor.name;12 const isGetterSetter = Object.getOwnPropertyDescriptor(target, key);13 if (isGetterSetter) {14 if (isGetterSetter.get) {15 if (!data_1.virtuals[name]) {16 data_1.virtuals[name] = {};17 }18 if (!data_1.virtuals[name][key]) {19 data_1.virtuals[name][key] = {};20 }21 data_1.virtuals[name][key] = Object.assign({}, data_1.virtuals[name][key], { get: isGetterSetter.get });22 }23 if (isGetterSetter.set) {24 if (!data_1.virtuals[name]) {25 data_1.virtuals[name] = {};26 }27 if (!data_1.virtuals[name][key]) {28 data_1.virtuals[name][key] = {};29 }30 data_1.virtuals[name][key] = Object.assign({}, data_1.virtuals[name][key], { set: isGetterSetter.set });31 }32 return;33 }34 if (isArray) {35 utils_1.initAsArray(name, key);36 }37 else {38 utils_1.initAsObject(name, key);39 }40 const ref = rawOptions.ref;41 if (ref) {42 data_1.schema[name][key] = Object.assign({}, data_1.schema[name][key], { type: mongoose.Schema.Types.ObjectId, ref: ref.name });43 return;44 }45 const itemsRef = rawOptions.itemsRef;46 if (itemsRef) {47 data_1.schema[name][key][0] = Object.assign({}, data_1.schema[name][key][0], { type: mongoose.Schema.Types.ObjectId, ref: itemsRef.name });48 return;49 }50 const enumOption = rawOptions.enum;51 if (enumOption) {52 if (!Array.isArray(enumOption)) {53 rawOptions.enum = Object.keys(enumOption).map((propKey) => enumOption[propKey]);54 }55 }56 // check for validation inconsistencies57 if (isWithStringValidate(rawOptions) && !utils_1.isString(Type)) {58 throw new errors_1.NotStringTypeError(key);59 }60 if (isWithNumberValidate(rawOptions) && !utils_1.isNumber(Type)) {61 throw new errors_1.NotNumberTypeError(key);62 }63 const instance = new Type();64 const subSchema = data_1.schema[instance.constructor.name];65 if (!subSchema && !(utils_1.isPrimitive(Type) || utils_1.isMongoose(Type) || utils_1.isAny(Type))) {66 throw new errors_1.InvalidPropError(Type.name, key);67 }68 const options = _.omit(rawOptions, ['ref', 'items']);69 if (utils_1.isPrimitive(Type) || utils_1.isMongoose(Type)) {70 if (isArray) {71 data_1.schema[name][key][0] = Object.assign({}, data_1.schema[name][key][0], options, { type: Type });72 return;73 }74 data_1.schema[name][key] = Object.assign({}, data_1.schema[name][key], options, { type: Type });75 return;76 }77 if (utils_1.isAny(Type)) {78 if (isArray) {79 data_1.schema[name][key][0] = Object.assign({}, data_1.schema[name][key][0], options, { type: mongoose.Schema.Types.Mixed });80 return;81 }82 data_1.schema[name][key] = Object.assign({}, data_1.schema[name][key], options, { type: mongoose.Schema.Types.Mixed });83 return;84 }85 if (isArray) {86 data_1.schema[name][key][0] = Object.assign({}, data_1.schema[name][key][0], options, subSchema);87 return;88 }89 data_1.schema[name][key] = Object.assign({}, data_1.schema[name][key], options, subSchema);90 return;91};92exports.prop = (options = {}) => (target, key) => {93 const Type = Reflect.getMetadata('design:type', target, key);94 if (!Type) {95 throw new errors_1.NoMetadataError(key);96 }97 baseProp(options, Type, target, key);98};99exports.arrayProp = (options) => (target, key) => {100 const Type = options.items;101 baseProp(options, Type, target, key, true);...
polyfills.js
Source:polyfills.js
1;2/**3 * @fileoverview Polyfills for old browsers.4 * @see https://github.com/inexorabletash/polyfill/blob/master/es5.js5 * @see https://gist.github.com/jhermsmeier/9a34b06a107bbf5d2c916 */7// ES 15.2.3.6 Object.defineProperty ( O, P, Attributes )8// Partial support for most common case - getters, setters, and values9(function() {10 if (!Object.defineProperty ||11 !(function () { try { Object.defineProperty({}, 'x', {}); return true; } catch (e) { return false; } } ())) {12 var orig = Object.defineProperty;13 Object.defineProperty = function (o, prop, desc) {14 // In IE8 try built-in implementation for defining properties on DOM prototypes.15 if (orig) { try { return orig(o, prop, desc); } catch (e) {} }16 if (o !== Object(o)) { throw TypeError("Object.defineProperty called on non-object"); }17 if (Object.prototype.__defineGetter__ && ('get' in desc)) {18 Object.prototype.__defineGetter__.call(o, prop, desc.get);19 }20 if (Object.prototype.__defineSetter__ && ('set' in desc)) {21 Object.prototype.__defineSetter__.call(o, prop, desc.set);22 }23 if ('value' in desc) {24 o[prop] = desc.value;25 }26 return o;27 };28 }29}());30// On older versions of IE Object.getOwnPropertyDescriptor can only be31// called with DOM elements; Here it is tested against a non-DOM object.32// If an error is raised, the method is replaced.33// https://gist.github.com/jhermsmeier/9a34b06a107bbf5d2c9134try {35 Object.getOwnPropertyDescriptor({"t":"o"}, "t");36} catch(err) {37 Object.getOwnPropertyDescriptor = function( object, key ) {38 39 var hasSupport =40 typeof object.__lookupGetter__ === 'function' &&41 typeof object.__lookupSetter__ === 'function'42 43 // TODO: How does one determine this?!44 var isGetterSetter = !hasSupport ? null :45 object.__lookupGetter__( key ) ||46 object.__lookupSetter__( key )47 48 return isGetterSetter != null ? {49 configurable: true,50 enumerable: true,51 get: object.__lookupGetter__( key ),52 set: object.__lookupSetter__( key )53 } : {54 configurable: true,55 writable: true,56 enumerable: true,57 value: object[ key ]58 }59 }...
Using AI Code Generation
1import { isGetterSetter } from 'ng-mocks';2import { isGetterSetter } from 'ng-mocks';3import { isGetterSetter } from 'ng-mocks';4const mock = { a: 1 };5import { isGetterSetter } from 'ng-mocks';6import { isGetterSetter } from 'ng-mocks';7import { isGetterSetter } from 'ng-mocks';8import { isGetterSetter } from 'ng-mocks';9import { isGetterSetter } from 'ng-mocks';10import { isGetterSetter } from 'ng-mocks';11import { isGetterSetter } from 'ng-mocks';12import { isGetterSetter } from 'ng-mocks';13import { isGetterSetter } from 'ng-mocks';14import { isGetterSetter } from 'ng-mocks';15import { isGetterSetter } from 'ng-mocks';16import { isGetterSetter } from 'ng-mocks';17isGetterSetter(mock, 'l
Using AI Code Generation
1import { isGetterSetter } from 'ng-mocks';2class Test {3 private _test: string;4 get test(): string {5 return this._test;6 }7 set test(value: string) {8 this._test = value;9 }10}11const test = new Test();12test.test = 'test';13import { isGetterSetter } from 'ng-mocks';14class Test {15 private _test: string;16 get test(): string {17 return this._test;18 }19 set test(value: string) {20 this._test = value;21 }22}23const test = new Test();24test.test = 'test';25it('should return true', () => {26 expect(isGetterSetter(test, 'test')).toBe(true);27});28isProperty(object, property)29import { isProperty } from 'ng-mocks';30class Test {31 private _test: string;32 get test(): string {33 return this._test;34 }35 set test(value: string) {36 this._test = value;37 }38}39const test = new Test();40test.test = 'test';41import { isProperty } from 'ng-mocks';42class Test {43 private _test: string;44 get test(): string {45 return this._test;46 }47 set test(value: string) {48 this._test = value;49 }50}51const test = new Test();52test.test = 'test';53it('should return false', () => {54 expect(isProperty(test, 'test')).toBe(false);55});56isSpy(object)
Using AI Code Generation
1import { isGetterSetter } from 'ng-mocks';2describe('isGetterSetter', () => {3 it('should return true if the given object is a getter/setter', () => {4 const obj = {5 get prop() {6 return 'value';7 },8 };9 expect(isGetterSetter(obj, 'prop')).toBe(true);10 });11 it('should return false if the given object is not a getter/setter', () => {12 const obj = {13 };14 expect(isGetterSetter(obj, 'prop')).toBe(false);15 });16});17export { isGetterSetter } from 'ng-mocks';
Using AI Code Generation
1import { isGetterSetter } from 'ng-mocks';2import { Component } from '@angular/core';3@Component({4})5export class TestComponent {6 private _test: string;7 get test(): string {8 return this._test;9 }10 set test(value: string) {11 this._test = value;12 }13}14describe('TestComponent', () => {15 it('should be created', () => {16 const component = new TestComponent();17 expect(component).toBeDefined();18 expect(isGetterSetter(component, 'test')).toBeTruthy();19 expect(isGetterSetter(component, 'test2')).toBeFalsy();20 });21});22import { isGetterSetter } from 'ng-mocks';23describe('TestComponent', () => {24 it('should be created', () => {25 const component = new TestComponent();26 expect(component).toBeDefined();27 expect(isGetterSetter(component, 'test')).toBeTruthy();28 expect(isGetterSetter(component, 'test2')).toBeFalsy();29 });30});31isGetterSetter(
Using AI Code Generation
1import { isGetterSetter } from 'ng-mocks';2const obj = {3 get test() {4 return 'test';5 }6};7describe('isGetterSetter', () => {8 it('should return true', () => {9 expect(isGetterSetter(obj, 'test')).toBe(true);10 });11});12describe('isGetterSetter', () => {13 it('should return false', () => {14 expect(isGetterSetter(obj, 'test1')).toBe(false);15 });16});17 ✓ should return true (2ms)18 ✓ should return false (1ms)19 ✓ should return true (2ms)20 ✓ should return false (1ms)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!