Best JavaScript code snippet using ts-auto-mock
util.js
Source:util.js
1// Utility methods - TODO refactor2angular.module('sisui')3.factory('SisUtil', function($window, $state, SisUser, $q) {4 "use strict";5 // add some utilities to the client6 function getArrayDescriptor(arr, name) {7 var res = {8 type : "Array"9 };10 if (arr.length && arr[0]) {11 res.children = [normalizeDescriptor(arr[0], null)];12 if (res.children[0].enum) {13 res.enum = res.children[0].enum;14 delete res.children[0].enum;15 }16 } else {17 res.children = [{ "type" : "Mixed" }];18 }19 if (name) {20 res.name = name;21 }22 res.children.forEach(function(c) {23 c._parent_ = res;24 });25 return res;26 }27 function fixChildren(desc) {28 desc.children.forEach(function(c) {29 c._parent_ = desc;30 });31 desc.children.sort(function(c1, c2) {32 if (c1.name < c2.name) { return -1 ;}33 else if (c1.name > c2.name) { return 1 ;}34 return 0;35 });36 }37 function normalizeDescriptor(desc, name) {38 var k, inner = null;39 if (desc instanceof Array) {40 return getArrayDescriptor(desc, name);41 } else if (typeof desc === "string") {42 return { type : desc, name : name };43 } else if ('type' in desc) {44 if (typeof desc.type === "string") {45 var result = {46 name : name47 };48 for (k in desc) {49 result[k] = desc[k];50 }51 if (desc.type == "ObjectId" && desc.ref) {52 result.type = desc.type;53 result.ref = desc.ref;54 }55 return result;56 } else {57 // check if it's an array58 if (desc.type instanceof Array) {59 inner = getArrayDescriptor(desc.type, name);60 for (k in desc) {61 if (k != 'type') {62 inner[k] = desc[k];63 }64 }65 return inner;66 } else {67 // desc.type is an object68 if (typeof desc.type.type === "string") {69 // this implies that desc.type70 // is a descriptor and desc itself71 // is an embedded schema72 inner = {73 name : name,74 type : "Document",75 children : getDescriptors(desc)76 };77 fixChildren(inner);78 return inner;79 } else {80 // desc.type is the embedded schema81 // definition82 inner = {83 name : name,84 type : "Document",85 children : getDescriptors(desc.type)86 };87 fixChildren(inner);88 for (k in desc) {89 if (k != 'type') {90 inner[k] = desc[k];91 }92 }93 return inner;94 }95 }96 }97 } else {98 // embedded scema99 inner = {100 name : name,101 type : "Document",102 children : getDescriptors(desc)103 };104 inner.children.map(function(c) {105 c._parent_ = inner;106 });107 return inner;108 }109 }110 function _getPathForDesc(desc) {111 var paths = [];112 while (desc) {113 if (desc.name) {114 paths.push(desc.name);115 } else {116 paths.push('_0');117 }118 desc = desc._parent_;119 }120 paths.reverse();121 return paths;122 }123 function getDescriptorToJSON(desc) {124 return function() {125 var result = { };126 for (var k in desc) {127 if (k != 'toJSON' && k != '_parent_') {128 result[k] = desc[k];129 }130 }131 return result;132 };133 }134 function getDescriptors(defn) {135 if (!defn) { return []; }136 var result = [];137 for (var k in defn) {138 var desc = defn[k];139 var normalized = normalizeDescriptor(desc, k);140 normalized.toJSON = getDescriptorToJSON(desc);141 result.push(normalized);142 }143 result.sort(function(c1, c2) {144 if (c1.name < c2.name) { return -1 ;}145 else if (c1.name > c2.name) { return 1 ;}146 return 0;147 });148 return result;149 }150 var _canAddEntityForSchema = function(schema) {151 var user = SisUser.getCurrentUser();152 if (!user) {153 return false;154 }155 if (user.super_user) { return true; }156 var roles = user.roles || { };157 if (!Object.keys(roles).length) {158 return false;159 }160 if (schema.is_open || schema.is_public) {161 return true;162 }163 var owner = schema._sis.owner;164 for (var i = 0; i < owner.length; ++i) {165 if (owner[i] in roles) {166 return true;167 }168 }169 return false;170 };171 var _getAdminRoles = function() {172 var user = SisUser.getCurrentUser();173 if (!user) {174 return null;175 }176 if (user.super_user) {177 return true;178 }179 var roles = user.roles || { };180 var result = [];181 for (var k in roles) {182 if (roles[k] == 'admin') {183 result.push(k);184 }185 }186 return result;187 };188 var _getAllRoles = function() {189 var user = SisUser.getCurrentUser();190 if (!user) {191 return [];192 }193 if (user.super_user) {194 return true;195 }196 var roles = user.roles || { };197 return Object.keys(roles);198 };199 var _getOwnerSubset = function(schema) {200 var user = SisUser.getCurrentUser();201 if (!user) {202 return [];203 }204 if (user.super_user) {205 return schema._sis.owner;206 }207 var roles = user.roles || { };208 var subset = schema._sis.owner.filter(function(o) {209 return o in roles;210 });211 return subset;212 };213 var _canDelete = function(obj) {214 return obj && !(obj._sis && obj._sis.locked);215 };216 var _canManageEntity = function(entity, schema) {217 var user = SisUser.getCurrentUser();218 if (!user) {219 return false;220 }221 if (user.super_user) { return true; }222 var roles = user.roles || { };223 var meta = entity._sis || { };224 var owner = meta.owner || schema._sis.owner;225 var anyOwnerCanModify = meta.any_owner_can_modify;226 var numGroups = 0;227 for (var i = 0; i < owner.length; ++i) {228 var group = owner[i];229 if (roles[group]) {230 numGroups++;231 }232 }233 return numGroups === owner.length ||234 (anyOwnerCanModify && numGroups > 0);235 };236 var _canManageSchema = function(schema) {237 var user = SisUser.getCurrentUser();238 if (!user) {239 return false;240 }241 if (user.super_user) { return true; }242 if (schema.is_open) { return true; }243 var roles = user.roles || { };244 var numAdminRoles = 0;245 var owners = schema._sis.owner;246 for (var i = 0; i < owners.length; ++i) {247 var group = owners[i];248 if (roles[group] == 'admin') {249 numAdminRoles++;250 }251 }252 return (numAdminRoles == owners.length ||253 numAdminRoles > 0 && schema.any_owner_can_modify);254 };255 var _getObjectField = function(object, field) {256 if (!object || !field) {257 return null;258 }259 var fields = field.split(".");260 if (fields.length === 1) {261 return object[field];262 }263 var obj = object;264 var f = null;265 while (obj && fields.length > 1) {266 f = fields.shift();267 obj = obj[f];268 }269 if (!obj) {270 return null;271 }272 f = fields.shift();273 return obj[f];274 };275 var _getIdField = function(schema) {276 var defn = schema.definition;277 var descriptor = null;278 if (schema.id_field !== '_id') {279 var idField = schema.id_field;280 if (idField.split(".").length === 1) {281 // ok - now check the type282 descriptor = defn[idField];283 if (descriptor.type === "IpAddress") {284 return idField + ".ip_address";285 } else {286 return idField;287 }288 } // fall through for now... TODO fix289 }290 for (var k in defn) {291 if (typeof defn[k] === 'object') {292 descriptor = defn[k];293 if (typeof(descriptor.type) === "string" &&294 descriptor.type == "String" &&295 descriptor.required &&296 descriptor.unique) {297 // found a required, unique string298 return k;299 }300 }301 }302 var result = "_id";303 if ('name' in defn) {304 result = "name";305 } else if ("title" in defn) {306 result = "title";307 }308 return result;309 };310 var _getNewItemForDesc = function(desc) {311 if (desc.type == "Document") {312 return { };313 } else if (desc.type == "Array") {314 return [];315 } else if (desc.enum && desc.enum.length) {316 return desc.enum[0];317 } else if (desc.type == "ObjectId") {318 return null;319 } else {320 return "";321 }322 };323 var _getInputType = function(type) {324 switch (type) {325 case "Boolean":326 return "checkbox";327 case "Number":328 return "number";329 default:330 return "text";331 }332 };333 var _descriptorTypes = ["Boolean", "String", "Number", "Mixed",334 "Document", "Array", "ObjectId", "IpAddress"];335 var attrsCache = { };336 var _getAttributesForType = function(type) {337 if (attrsCache[type]) {338 return attrsCache[type];339 }340 var result = [341 {name : 'required', type : 'checkbox'},342 {name : 'unique', type : 'checkbox'},343 {name : 'comment', type : 'text'}344 ];345 switch (type) {346 case "Number":347 result.push({ name : "min", type : "number" });348 result.push({ name : "max", type : "number" });349 break;350 case "String":351 result.push({ name : "lowercase", type : "checkbox" });352 result.push({ name : "trim", type : "checkbox" });353 result.push({ name : "uppercase", type : "checkbox" });354 result.push({ name : "enum", type : "textArray" });355 result.push({ name : "match", type : "regex" });356 result.push({ name : "code", type : "text" });357 break;358 case "ObjectId":359 result.push({ name : "ref", type : "select", values : "schemaList" });360 break;361 default:362 break;363 }364 attrsCache[type] = result;365 return result;366 };367 var _getScriptSchema = function() {368 return {369 name : "sis_scripts",370 _sis : {371 owner : _getAllRoles()372 },373 definition : {374 name : { type : "String", required : true, unique : true, match : /^[a-z0-9_\-]+$/ },375 description : { type : "String" },376 script_type : { type: "String", required : true, enum : ["application/javascript"] },377 script : { type: "String", code : true, code_type_field : "script_type" }378 }379 };380 };381 var _getHookSchema = function() {382 return {383 name : "sis_hooks",384 _sis : {385 owner : _getAllRoles()386 },387 definition : {388 name : { type : "String", required : true, unique : true, match : '/^[0-9a-z_]+$/' },389 target : {390 type : {391 url : { type : "String", required : true },392 action : { type : "String", required : true, enum : ["GET", "POST", "PUT"]}393 },394 required : true395 },396 retry_count : { type : "Number", min : 0, max : 20, "default" : 0 },397 retry_delay : { type : "Number", min : 1, max : 60, "default" : 1 },398 events : { type : [{ type : "String", enum : ["insert", "update", "delete"] }], required : true },399 entity_type : { type : "String", required : true }400 }401 };402 };403 var _goBack = function(state) {404 if ($window.history.length) {405 $window.history.back();406 } else {407 $state.go(state);408 }409 };410 var _toRegex = function(str) {411 try {412 if (str instanceof RegExp) {413 return str;414 }415 if (!str || str[0] != '/') {416 return null;417 }418 var splits = str.split('/');419 if (splits.length < 3 || splits[0]) {420 return null;421 }422 var flags = splits.pop();423 splits.shift();424 var regex = splits.join("/");425 if (!regex) {426 return null;427 }428 return new RegExp(regex, flags);429 } catch(ex) {430 }431 return null;432 };433 var _getSisMetaDescriptor = function() {434 var children = [435 { name : "locked", type : "Boolean" },436 { name : "immutable", type : "Boolean" },437 { name : "tags", type : "String" },438 { name : "owner", type : ["String"] }439 ];440 var meta = {441 name : '_sis', type : 'Document', children : children442 };443 meta.children.forEach(function(c) {444 c._parent_ = meta;445 });446 return meta;447 };448 var _toStringArray = function(text) {449 if (text instanceof Array) {450 return text;451 }452 return text.split(",").map(function(str) {453 return str.trim();454 }).filter(function(str) {455 return str !== "";456 });457 };458 return {459 getDescriptorArray : function(schema) {460 return getDescriptors(schema.definition);461 },462 getIdField : _getIdField,463 getObjectField : _getObjectField,464 canManageEntity : _canManageEntity,465 canManageSchema : _canManageSchema,466 canAddEntity : _canAddEntityForSchema,467 getDescriptorPath : _getPathForDesc,468 getNewItemForDesc : _getNewItemForDesc,469 canDelete : _canDelete,470 getOwnerSubset : _getOwnerSubset,471 getAdminRoles : _getAdminRoles,472 getAllRoles : _getAllRoles,473 getInputType : _getInputType,474 descriptorTypes : _descriptorTypes,475 attributesForType : _getAttributesForType,476 getHookSchema : _getHookSchema,477 getScriptSchema : _getScriptSchema,478 goBack : _goBack,479 toRegex : _toRegex,480 getSisMetaDescriptor : _getSisMetaDescriptor,481 toStringArray : _toStringArray482 };...
descriptor.ts
Source:descriptor.ts
...160 node as ts.ParenthesizedTypeNode,161 scope162 );163 case core.ts.SyntaxKind.ArrayType:164 return GetArrayDescriptor();165 case core.ts.SyntaxKind.TupleType:166 return GetTupleDescriptor(node as ts.TupleTypeNode, scope);167 case core.ts.SyntaxKind.StringKeyword:168 return GetStringDescriptor();169 case core.ts.SyntaxKind.NumberKeyword:170 return GetNumberDescriptor();171 case core.ts.SyntaxKind.TrueKeyword:172 return GetBooleanTrueDescriptor();173 case core.ts.SyntaxKind.FalseKeyword:174 return GetBooleanFalseDescriptor();175 case core.ts.SyntaxKind.NumericLiteral:176 case core.ts.SyntaxKind.StringLiteral:177 return GetLiteralDescriptor(node as ts.LiteralTypeNode, scope);178 case core.ts.SyntaxKind.ObjectLiteralExpression:...
array.ts
Source:array.ts
1import type * as ts from 'typescript';2import { createArrayLiteral } from '../../../typescriptFactory/typescriptFactory';3export function GetArrayDescriptor(): ts.Expression {4 return createArrayLiteral();...
Using AI Code Generation
1import { GetArrayDescriptor } from 'ts-auto-mock';2import { GetArrayDescriptor } from 'ts-auto-mock';3import { GetArrayDescriptor } from 'ts-auto-mock';4import { GetArrayDescriptor } from 'ts-auto-mock';5import { GetArrayDescriptor } from 'ts-auto-mock';6import { GetArrayDescriptor } from 'ts-auto-mock';7import { GetArrayDescriptor } from 'ts-auto-mock';8import { GetArrayDescriptor } from 'ts-auto-mock';9import { GetArrayDescriptor } from 'ts-auto-mock';10import { GetArrayDescriptor } from 'ts-auto-mock';11import { GetArrayDescriptor } from 'ts-auto-mock';12import { GetArrayDescriptor } from 'ts-auto-mock';13import { GetArrayDescriptor } from 'ts-auto-mock';14export interface ITest {15 a: string;16}17describe('test', () => {18 it('test', () => {19 const mock = GetArrayDescriptor<ITest>();20 console.log(mock);21 });22});23"compilerOptions": {
Using AI Code Generation
1import { GetArrayDescriptor } from 'ts-auto-mock';2const descriptor = GetArrayDescriptor('string');3import { GetArrayDescriptor } from 'ts-auto-mock';4const descriptor = GetArrayDescriptor('number');5import { GetArrayDescriptor } from 'ts-auto-mock';6const descriptor = GetArrayDescriptor('boolean');7import { GetArrayDescriptor } from 'ts-auto-mock';8const descriptor = GetArrayDescriptor('Date');9import { GetArrayDescriptor } from 'ts-auto-mock';10const descriptor = GetArrayDescriptor('MyClass');11import { GetArrayDescriptor } from 'ts-auto-mock';12const descriptor = GetArrayDescriptor('MyInterface');13import { GetArrayDescriptor } from 'ts-auto-mock';14const descriptor = GetArrayDescriptor('MyEnum');15import { GetArrayDescriptor } from 'ts-auto-mock';16const descriptor = GetArrayDescriptor('MyType');17import { GetArrayDescriptor } from 'ts-auto-mock';18const descriptor = GetArrayDescriptor('MyClass[]');19import { GetArrayDescriptor } from 'ts-auto-mock
Using AI Code Generation
1const tsAutoMock = require('ts-auto-mock');2const arrayDescriptor = tsAutoMock.GetArrayDescriptor();3console.log(arrayDescriptor);4const tsAutoMock = require('ts-auto-mock');5const arrayDescriptor = tsAutoMock.GetArrayDescriptor();6console.log(arrayDescriptor);7const tsAutoMock = require('ts-auto-mock');8const arrayDescriptor = tsAutoMock.GetArrayDescriptor();9console.log(arrayDescriptor);
Using AI Code Generation
1import { GetArrayDescriptor } from 'ts-auto-mock'2const arrayDescriptor = GetArrayDescriptor([1, 2, 3])3console.log(arrayDescriptor)4import { GetArrayDescriptor } from 'ts-auto-mock'5const arrayDescriptor = GetArrayDescriptor(['a', 'b', 'c'])6console.log(arrayDescriptor)7import { GetArrayDescriptor } from 'ts-auto-mock'8const arrayDescriptor = GetArrayDescriptor([{ a: 1, b: 2 }, { a: 3, b: 4 }])9console.log(arrayDescriptor)10import { GetArrayDescriptor } from 'ts-auto-mock'11const arrayDescriptor = GetArrayDescriptor([{ a: 1, b: 'c' }, { a: 3, b: 'd' }])12console.log(arrayDescriptor)13import { GetArrayDescriptor } from 'ts-auto-mock'14const arrayDescriptor = GetArrayDescriptor([{ a: 1, b: 'c' }, { a: 3, b: 'd' }, { a: 5, b: 'e' }])15console.log(arrayDescriptor)
Using AI Code Generation
1import { GetArrayDescriptor } from 'ts-auto-mock/extension';2import { ArrayDescriptor } from 'ts-auto-mock/extension';3const descriptor = GetArrayDescriptor<ArrayDescriptor<number>>();4console.log(descriptor);5{6 items: {7 }8}9import { GetArrayDescriptor } from 'ts-auto-mock/extension';10import { ArrayDescriptor } from 'ts-auto-mock/extension';11const descriptor = GetArrayDescriptor<ArrayDescriptor<number>>();12console.log(descriptor);13{14 items: {15 }16}17{18 items: {19 }20}
Using AI Code Generation
1const GetArrayDescriptor = require('ts-auto-mock/extension').GetArrayDescriptor;2const arrayDescriptor = GetArrayDescriptor({3});4console.log(arrayDescriptor);5const GetArrayDescriptor = require('ts-auto-mock/extension').GetArrayDescriptor;6const arrayDescriptor = GetArrayDescriptor({7});8console.log(arrayDescriptor);9const GetArrayDescriptor = require('ts-auto-mock/extension').GetArrayDescriptor;10const arrayDescriptor = GetArrayDescriptor({11 properties: {12 foo: { type: 'string' },13 bar: { type: 'number' },14 },15});16console.log(arrayDescriptor);17const GetArrayDescriptor = require('ts-auto-mock/extension').GetArrayDescriptor;18const arrayDescriptor = GetArrayDescriptor({19 properties: {20 foo: { type: 'string' },21 bar: { type: 'number' },22 },23});24console.log(arrayDescriptor);25const GetArrayDescriptor = require('ts-auto-mock/extension').GetArrayDescriptor;
Using AI Code Generation
1import { GetArrayDescriptor } from '../../src';2import { test } from 'ava';3test('GetArrayDescriptor', t => {4 const descriptor = GetArrayDescriptor('string');5 t.deepEqual(descriptor, {6 items: {7 }8 });9});10import { GetArrayDescriptor } from '../../src';11import { test } from 'ava';12test('GetArrayDescriptor', t => {13 const descriptor = GetArrayDescriptor('number');14 t.deepEqual(descriptor, {15 items: {16 }17 });18});19test('GetArrayDescriptor', t => {20 const descriptor = GetArrayDescriptor('boolean');21 t.deepEqual(descriptor, {22 items: {23 }24 });25});26test('GetArrayDescriptor', t => {27 const descriptor = GetArrayDescriptor('object');28 t.deepEqual(descriptor, {29 items: {30 }31 });32});33test('GetArrayDescriptor', t => {34 const descriptor = GetArrayDescriptor('string');35 t.deepEqual(descriptor, {36 items: {37 }38 });39});40test('GetArrayDescriptor', t => {41 const descriptor = GetArrayDescriptor('any');42 t.deepEqual(descriptor, {43 items: {44 }45 });46});47test('GetArrayDescriptor', t => {48 const descriptor = GetArrayDescriptor('Array');49 t.deepEqual(descriptor, {50 items: {51 }52 });53});54test('GetArrayDescriptor', t => {55 const descriptor = GetArrayDescriptor('Array<string>');56 t.deepEqual(descriptor, {57 items: {58 }59 });60});61test('GetArrayDescriptor', t => {62 const descriptor = GetArrayDescriptor('Array<number>');63 t.deepEqual(descriptor, {64 items: {65 }66 });67});68test('GetArrayDescriptor', t => {69 const descriptor = GetArrayDescriptor('Array<boolean>');70 t.deepEqual(descriptor,
Using AI Code Generation
1import { GetArrayDescriptor } from 'ts-auto-mock';2import { mock } from 'ts-auto-mock';3const arrayDescriptor = GetArrayDescriptor(Number);4const mockOfArray = mock(arrayDescriptor);5console.log(mockOfArray);6import { GetArrayDescriptor } from 'ts-auto-mock';7import { mock } from 'ts-auto-mock';8const arrayDescriptor = GetArrayDescriptor(String);9const mockOfArray = mock(arrayDescriptor);10console.log(mockOfArray);11import { GetArrayDescriptor } from 'ts-auto-mock';12import { mock } from 'ts-auto-mock';13const arrayDescriptor = GetArrayDescriptor(Boolean);14const mockOfArray = mock(arrayDescriptor);15console.log(mockOfArray);
Using AI Code Generation
1import { GetArrayDescriptor } from 'ts-auto-mock';2import { TestInterface } from './testInterface';3const arrayDescriptor = GetArrayDescriptor(TestInterface);4import { GetArrayDescriptor } from 'ts-auto-mock';5import { TestType } from './testType';6const arrayDescriptor = GetArrayDescriptor(TestType);7import { GetArrayDescriptor } from 'ts-auto-mock';8import { TestClass } from './testClass';9const arrayDescriptor = GetArrayDescriptor(TestClass);10import { GetArrayDescriptor } from 'ts-auto-mock';11import { TestClass } from './testClass';12const arrayDescriptor = GetArrayDescriptor(TestClass, 2);13import { GetArrayDescriptor } from 'ts-auto-mock';14import { TestClass } from './testClass';15const arrayDescriptor = GetArrayDescriptor(TestClass, 2, 4);16import { GetArrayDescriptor } from 'ts-auto-mock';17import { TestClass } from './testClass';18const arrayDescriptor = GetArrayDescriptor(TestClass, 2, 4, 6);
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!!