Best JavaScript code snippet using best
object.js
Source:object.js
1const assert = require('assert');2const ObjectMD = require('../../../lib/models/ObjectMD');3const constants = require('../../../lib/constants');4describe('ObjectMD class setters/getters', () => {5 let md = null;6 beforeEach(() => {7 md = new ObjectMD();8 });9 [10 // In order: data property, value to set/get, default value11 ['OwnerDisplayName', null, ''],12 ['OwnerDisplayName', 'owner-display-name'],13 ['OwnerId', null, ''],14 ['OwnerId', 'owner-id'],15 ['CacheControl', null, ''],16 ['CacheControl', 'cache-control'],17 ['ContentDisposition', null, ''],18 ['ContentDisposition', 'content-disposition'],19 ['ContentEncoding', null, ''],20 ['ContentEncoding', 'content-encoding'],21 ['Expires', null, ''],22 ['Expires', 'expire-date'],23 ['ContentLength', null, 0],24 ['ContentLength', 15000],25 ['ContentType', null, ''],26 ['ContentType', 'content-type'],27 ['LastModified', new Date().toJSON()],28 ['ContentMd5', null, ''],29 ['ContentMd5', 'content-md5'],30 ['AmzVersionId', null, 'null'],31 ['AmzVersionId', 'version-id'],32 ['AmzServerVersionId', null, ''],33 ['AmzServerVersionId', 'server-version-id'],34 ['AmzStorageClass', null, 'STANDARD'],35 ['AmzStorageClass', 'storage-class'],36 ['AmzServerSideEncryption', null, ''],37 ['AmzServerSideEncryption', 'server-side-encryption'],38 ['AmzEncryptionKeyId', null, ''],39 ['AmzEncryptionKeyId', 'encryption-key-id'],40 ['AmzEncryptionCustomerAlgorithm', null, ''],41 ['AmzEncryptionCustomerAlgorithm', 'customer-algorithm'],42 ['Acl', null, {43 Canned: 'private',44 FULL_CONTROL: [],45 WRITE_ACP: [],46 READ: [],47 READ_ACP: [],48 }],49 ['Acl', {50 Canned: 'public',51 FULL_CONTROL: ['id'],52 WRITE_ACP: ['id'],53 READ: ['id'],54 READ_ACP: ['id'],55 }],56 ['Key', null, ''],57 ['Key', 'key'],58 ['Location', null, []],59 ['Location', ['location1']],60 ['IsNull', null, ''],61 ['IsNull', true],62 ['NullVersionId', null, ''],63 ['NullVersionId', '111111'],64 ['IsDeleteMarker', null, ''],65 ['IsDeleteMarker', true],66 ['VersionId', null, undefined],67 ['VersionId', '111111'],68 ['Tags', null, {}],69 ['Tags', {70 key: 'value',71 }],72 ['Tags', null, {}],73 ['ReplicationInfo', null, {74 status: '',75 backends: [],76 content: [],77 destination: '',78 storageClass: '',79 role: '',80 storageType: '',81 dataStoreVersionId: '',82 }],83 ['ReplicationInfo', {84 status: 'PENDING',85 backends: [{86 site: 'zenko',87 status: 'PENDING',88 dataStoreVersionId: 'a',89 }],90 content: ['DATA', 'METADATA'],91 destination: 'destination-bucket',92 storageClass: 'STANDARD',93 role: 'arn:aws:iam::account-id:role/src-resource,' +94 'arn:aws:iam::account-id:role/dest-resource',95 storageType: 'aws_s3',96 dataStoreVersionId: '',97 }],98 ['DataStoreName', null, ''],99 ].forEach(test => {100 const property = test[0];101 const testValue = test[1];102 const defaultValue = test[2];103 const testName = testValue === null ? 'get default' : 'get/set';104 it(`${testName}: ${property}`, () => {105 if (testValue !== null) {106 md[`set${property}`](testValue);107 }108 const value = md[`get${property}`]();109 if ((testValue !== null && typeof testValue === 'object') ||110 typeof defaultValue === 'object') {111 assert.deepStrictEqual(value, testValue || defaultValue);112 } else if (testValue !== null) {113 assert.strictEqual(value, testValue);114 } else {115 assert.strictEqual(value, defaultValue);116 }117 });118 });119 it('ObjectMD::setReplicationSiteStatus', () => {120 md.setReplicationInfo({121 backends: [{122 site: 'zenko',123 status: 'PENDING',124 dataStoreVersionId: 'a',125 }],126 });127 md.setReplicationSiteStatus('zenko', 'COMPLETED');128 assert.deepStrictEqual(md.getReplicationInfo().backends, [{129 site: 'zenko',130 status: 'COMPLETED',131 dataStoreVersionId: 'a',132 }]);133 });134 it('ObjectMD::getReplicationSiteStatus', () => {135 md.setReplicationInfo({136 backends: [{137 site: 'zenko',138 status: 'PENDING',139 dataStoreVersionId: 'a',140 }],141 });142 assert.strictEqual(md.getReplicationSiteStatus('zenko'), 'PENDING');143 });144 it('ObjectMD::setReplicationSiteDataStoreVersionId', () => {145 md.setReplicationInfo({146 backends: [{147 site: 'zenko',148 status: 'PENDING',149 dataStoreVersionId: 'a',150 }],151 });152 md.setReplicationSiteDataStoreVersionId('zenko', 'b');153 assert.deepStrictEqual(md.getReplicationInfo().backends, [{154 site: 'zenko',155 status: 'PENDING',156 dataStoreVersionId: 'b',157 }]);158 });159 it('ObjectMD::getReplicationSiteDataStoreVersionId', () => {160 md.setReplicationInfo({161 backends: [{162 site: 'zenko',163 status: 'PENDING',164 dataStoreVersionId: 'a',165 }],166 });167 assert.strictEqual(168 md.getReplicationSiteDataStoreVersionId('zenko'), 'a');169 });170});171describe('ObjectMD import from stored blob', () => {172 it('should export and import correctly the latest model version', () => {173 const md = new ObjectMD();174 const jsonMd = md.getSerialized();175 const importedRes = ObjectMD.createFromBlob(jsonMd);176 assert.ifError(importedRes.error);177 const importedMd = importedRes.result;178 assert.deepStrictEqual(md, importedMd);179 });180 it('should convert old location to new location', () => {181 const md = new ObjectMD();182 const value = md.getValue();183 value['md-model-version'] = 1;184 value.location = 'stringLocation';185 const jsonMd = JSON.stringify(value);186 const importedRes = ObjectMD.createFromBlob(jsonMd);187 assert.strictEqual(importedRes.error, undefined);188 const importedMd = importedRes.result;189 const valueImported = importedMd.getValue();190 assert.strictEqual(valueImported['md-model-version'],191 constants.mdModelVersion);192 assert.deepStrictEqual(valueImported.location,193 [{ key: 'stringLocation' }]);194 });195 it('should keep null location as is', () => {196 const md = new ObjectMD();197 const value = md.getValue();198 value.location = null;199 const jsonMd = JSON.stringify(value);200 const importedRes = ObjectMD.createFromBlob(jsonMd);201 assert.strictEqual(importedRes.error, undefined);202 const importedMd = importedRes.result;203 const valueImported = importedMd.getValue();204 assert.deepStrictEqual(valueImported.location, null);205 importedMd.setLocation([]);206 assert.deepStrictEqual(importedMd.getValue().location, null);207 });208 it('should add dataStoreName attribute if missing', () => {209 const md = new ObjectMD();210 const value = md.getValue();211 value['md-model-version'] = 2;212 delete value.dataStoreName;213 const jsonMd = JSON.stringify(value);214 const importedRes = ObjectMD.createFromBlob(jsonMd);215 assert.strictEqual(importedRes.error, undefined);216 const importedMd = importedRes.result;217 const valueImported = importedMd.getValue();218 assert.strictEqual(valueImported['md-model-version'],219 constants.mdModelVersion);220 assert.notStrictEqual(valueImported.dataStoreName, undefined);221 });222 it('should return undefined for dataStoreVersionId if no object location',223 () => {224 const md = new ObjectMD();225 const value = md.getValue();226 const jsonMd = JSON.stringify(value);227 const importedRes = ObjectMD.createFromBlob(jsonMd);228 assert.strictEqual(importedRes.error, undefined);229 const importedMd = importedRes.result;230 assert.strictEqual(importedMd.getDataStoreVersionId(), undefined);231 });232 it('should get dataStoreVersionId if saved in object location', () => {233 const md = new ObjectMD();234 const dummyLocation = {235 dataStoreVersionId: 'data-store-version-id',236 };237 md.setLocation([dummyLocation]);238 const value = md.getValue();239 const jsonMd = JSON.stringify(value);240 const importedRes = ObjectMD.createFromBlob(jsonMd);241 assert.strictEqual(importedRes.error, undefined);242 const importedMd = importedRes.result;243 assert.strictEqual(importedMd.getDataStoreVersionId(),244 dummyLocation.dataStoreVersionId);245 });246 it('should return an error if blob is malformed JSON', () => {247 const importedRes = ObjectMD.createFromBlob('{BAD JSON}');248 assert.notStrictEqual(importedRes.error, undefined);249 assert.strictEqual(importedRes.result, undefined);250 });251});252describe('getAttributes static method', () => {253 it('should return object metadata attributes', () => {254 const attributes = ObjectMD.getAttributes();255 const expectedResult = {256 'owner-display-name': true,257 'owner-id': true,258 'cache-control': true,259 'content-disposition': true,260 'content-encoding': true,261 'expires': true,262 'content-length': true,263 'content-type': true,264 'content-md5': true,265 'x-amz-version-id': true,266 'x-amz-server-version-id': true,267 'x-amz-storage-class': true,268 'x-amz-server-side-encryption': true,269 'x-amz-server-side-encryption-aws-kms-key-id': true,270 'x-amz-server-side-encryption-customer-algorithm': true,271 'x-amz-website-redirect-location': true,272 'acl': true,273 'key': true,274 'location': true,275 'isNull': true,276 'nullVersionId': true,277 'isDeleteMarker': true,278 'versionId': true,279 'tags': true,280 'replicationInfo': true,281 'dataStoreName': true,282 'last-modified': true,283 'md-model-version': true };284 assert.deepStrictEqual(attributes, expectedResult);285 });...
getContent.ts
Source:getContent.ts
1import articles from '../../../../content/articles/articles.md';2import copy from '../../../../content/general/copy.md';3import dictionary from '../../../../content/general/dictionary.md';4import headings from '../../../../content/general/headings.md';5import tags from '../../../../content/general/tags.md';6import quickBits from '../../../../content/articles/quickBits.md';7import workExperience from '../../../../content/resume/workExperience.md';8import education from '../../../../content/resume/education.md';9import talks from '../../../../content/speaking/talks.md';10import { ArticleInterface, TagInterface, TalkInterface } from './Interfaces/Interfaces';11// Next.js and Storybook json loaders don't seem to work the same. Import could therfor be parsed and unparsed JSON12const parseJSONMD = (_: string | object) => typeof _ === 'string' ? JSON.parse(_) : _13const convertToKeyValue = (arr: any) => arr.reduce((acc: any, cur: any) => ({ ...acc, [cur.key]: cur.value }), {});14const populateTags = (tagsToPopulate: TagInterface[] = []) => tagsToPopulate15 .map((tagToPopulate: TagInterface | string) =>16 parseJSONMD(tags).attributes.items17 .find((tag: TagInterface) => tag.key === tagToPopulate) || null)18 .filter((tag: TagInterface) => tag !== null)19export const getArticles = () => parseJSONMD(articles).attributes.items.map((article: ArticleInterface) => ({ ...article, tags: populateTags(article.tags) }));20export const getCopy = () => convertToKeyValue(parseJSONMD(copy).attributes.items);21export const getDictionary = () => convertToKeyValue(parseJSONMD(dictionary).attributes.items)22export const getHeadings = () => convertToKeyValue(parseJSONMD(headings).attributes.items);23export const getTags = () => convertToKeyValue(parseJSONMD(tags).attributes.items);24export const getQuickBits = () => parseJSONMD(quickBits).attributes.items.map((quickBit: ArticleInterface) => ({ ...quickBit, tags: populateTags(quickBit.tags) }));25export const getFridayTips = () => [];26export const getWorkExperience = () => parseJSONMD(workExperience).attributes.items;27export const getEducation = () => parseJSONMD(education).attributes.items;...
Using AI Code Generation
1var jsonMd = require('json-md');2var fs = require('fs');3var json = fs.readFileSync('test.json', 'utf8');4var md = jsonMd(json);5fs.writeFile('test.md', md, function(err) {6 if (err) throw err;7 console.log('It\'s saved!');8});
Using AI Code Generation
1const Bestiary = require('./Bestiary');2const json = {3 "speed": {4 },5 "special_abilities": [{6 }, {7 }, {8 }],9 "actions": [{10 "desc": "Melee Weapon Attack: +5 to hit, reach 5 ft., one target. Hit: 7 (1d6 + 3) slashing damage.",11 }, {12 "desc": "Ranged Weapon Attack: +3 to hit, range 80/320 ft., one target. Hit: 5 (1d6 + 1) piercing damage
Using AI Code Generation
1var myModule = require('./module.js');2var data = myModule.jsonMd();3console.log(data);4var fs = require('fs');5fs.writeFile('test4.json', data, function (err) {6if (err) throw err;7console.log('It\'s saved!');8});9{10{11"name": "Samsung - 55\" Class (54.6\" Diag.) - LED - 2160p - Smart - 4K Ultra HD TV - Black",12"4K Ultra HD TVs (2160p)"
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!!