How to use camelize method in molecule

Best Python code snippet using molecule_python

normalize.js

Source:normalize.js Github

copy

Full Screen

1import camelCase from 'lodash/camelCase';2import isArray from 'lodash/isArray';3import isNull from 'lodash/isNull';4import keys from 'lodash/keys';5import merge from 'lodash/merge';6function wrap(json) {7 if (isArray(json)) {8 return json;9 }10 return [json];11}12function isDate(attributeValue) {13 return Object.prototype.toString.call(attributeValue) === '[object Date]';14}15function camelizeNestedKeys(attributeValue) {16 if (attributeValue === null || typeof attributeValue !== 'object' || isDate(attributeValue)) {17 return attributeValue;18 }19 if (isArray(attributeValue)) {20 return attributeValue.map(camelizeNestedKeys);21 }22 const copy = {};23 keys(attributeValue).forEach((k) => {24 copy[camelCase(k)] = camelizeNestedKeys(attributeValue[k]);25 });26 return copy;27}28function extractRelationships(relationships, { camelizeKeys, camelizeTypeValues }) {29 const ret = {};30 keys(relationships).forEach((key) => {31 const relationship = relationships[key];32 const name = camelizeKeys ? camelCase(key) : key;33 ret[name] = {};34 if (typeof relationship.data !== 'undefined') {35 if (isArray(relationship.data)) {36 ret[name].data = relationship.data.map(e => ({37 id: e.id,38 type: camelizeTypeValues ? camelCase(e.type) : e.type,39 }));40 } else if (!isNull(relationship.data)) {41 ret[name].data = {42 id: relationship.data.id,43 type: camelizeTypeValues ? camelCase(relationship.data.type) : relationship.data.type,44 };45 } else {46 ret[name].data = relationship.data;47 }48 }49 if (relationship.links) {50 ret[name].links = camelizeKeys ? camelizeNestedKeys(relationship.links) : relationship.links;51 }52 if (relationship.meta) {53 ret[name].meta = camelizeKeys ? camelizeNestedKeys(relationship.meta) : relationship.meta;54 }55 });56 return ret;57}58function processMeta(metaObject, { camelizeKeys }) {59 if (camelizeKeys) {60 const meta = {};61 keys(metaObject).forEach((key) => {62 meta[camelCase(key)] = camelizeNestedKeys(metaObject[key]);63 });64 return meta;65 }66 return metaObject;67}68function extractEntities(json, { camelizeKeys, camelizeTypeValues }) {69 const ret = {};70 wrap(json).forEach((elem) => {71 const type = camelizeKeys ? camelCase(elem.type) : elem.type;72 ret[type] = ret[type] || {};73 ret[type][elem.id] = ret[type][elem.id] || {74 id: elem.id,75 };76 ret[type][elem.id].type = camelizeTypeValues ? camelCase(elem.type) : elem.type;77 if (camelizeKeys) {78 ret[type][elem.id].attributes = {};79 keys(elem.attributes).forEach((key) => {80 ret[type][elem.id].attributes[camelCase(key)] = camelizeNestedKeys(elem.attributes[key]);81 });82 } else {83 ret[type][elem.id].attributes = elem.attributes;84 }85 if (elem.links) {86 ret[type][elem.id].links = {};87 keys(elem.links).forEach((key) => {88 const newKey = camelizeKeys ? camelCase(key) : key;89 ret[type][elem.id].links[newKey] = elem.links[key];90 });91 }92 if (elem.relationships) {93 ret[type][elem.id].relationships = extractRelationships(elem.relationships, {94 camelizeKeys,95 camelizeTypeValues,96 });97 }98 if (elem.meta) {99 ret[type][elem.id].meta = processMeta(elem.meta, { camelizeKeys });100 }101 });102 return ret;103}104function doFilterEndpoint(endpoint) {105 return endpoint.replace(/\?.*$/, '');106}107function extractMetaData(json, endpoint, { camelizeKeys, camelizeTypeValues, filterEndpoint }) {108 const ret = {};109 ret.meta = {};110 let metaObject;111 if (!filterEndpoint) {112 const filteredEndpoint = doFilterEndpoint(endpoint);113 ret.meta[filteredEndpoint] = {};114 ret.meta[filteredEndpoint][endpoint.slice(filteredEndpoint.length)] = {};115 metaObject = ret.meta[filteredEndpoint][endpoint.slice(filteredEndpoint.length)];116 } else {117 ret.meta[endpoint] = {};118 metaObject = ret.meta[endpoint];119 }120 metaObject.data = {};121 if (json.data) {122 const meta = [];123 wrap(json.data).forEach((object) => {124 const pObject = {125 id: object.id,126 type: camelizeTypeValues ? camelCase(object.type) : object.type,127 };128 if (object.relationships) {129 pObject.relationships = extractRelationships(object.relationships, {130 camelizeKeys,131 camelizeTypeValues,132 });133 }134 meta.push(pObject);135 });136 metaObject.data = meta;137 }138 if (json.links) {139 metaObject.links = json.links;140 ret.meta[doFilterEndpoint(endpoint)].links = json.links;141 }142 if (json.meta) {143 metaObject.meta = processMeta(json.meta, { camelizeKeys });144 }145 return ret;146}147export default function normalize(json, {148 filterEndpoint = true,149 camelizeKeys = true,150 camelizeTypeValues = true,151 endpoint,152} = {}) {153 const ret = {};154 if (json.data) {155 merge(ret, extractEntities(json.data, { camelizeKeys, camelizeTypeValues }));156 }157 if (json.included) {158 merge(ret, extractEntities(json.included, { camelizeKeys, camelizeTypeValues }));159 }160 if (endpoint) {161 const endpointKey = filterEndpoint ? doFilterEndpoint(endpoint) : endpoint;162 merge(ret, extractMetaData(json, endpointKey, {163 camelizeKeys,164 camelizeTypeValues,165 filterEndpoint,166 }));167 }168 return ret;...

Full Screen

Full Screen

camelize.py

Source:camelize.py Github

copy

Full Screen

...57 new[new_key] = _camelize_other_iterable(value, capitalized, strip_underscores)58 else:59 new[new_key] = value60 return new61def camelize(62 element: typing.Any,63 capitalized: bool = False,64 strip_underscores: bool = False,65 camelize_mapping_values: bool = False,66) -> typing.Any:67 if isinstance(element, typing.Mapping):68 return _camelize_mapping(element, capitalized, strip_underscores, camelize_mapping_values)69 elif isinstance(element, str):70 return _camelize_string(element, capitalized, strip_underscores)71 elif isinstance(element, typing.Iterable):72 return _camelize_other_iterable(element, capitalized, strip_underscores)...

Full Screen

Full Screen

test_camelize.py

Source:test_camelize.py Github

copy

Full Screen

1from collections import defaultdict2from datetime import date3from snakecamel import camelize4def test_camelize_empty_string() -> None:5 assert camelize("") == ""6def test_camelize_simple_string() -> None:7 assert camelize("snake_string") == "snakeString"8def test_campelize_simple_string_capitalized() -> None:9 assert camelize("snake_string", capitalized=True) == "SnakeString"10def test_camelize_outside_underscores() -> None:11 assert camelize("_snake_string__") == "_snakeString__"12def test_camelize_trim_outside_underscores() -> None:13 assert camelize("_snake_string__", strip_underscores=True) == "snakeString"14def test_camelize_trim_outside_underscores_capitalized() -> None:15 assert camelize("_snake_string__", strip_underscores=True, capitalized=True) == "SnakeString"16def test_camelize_simple_mapping() -> None:17 assert camelize({"simple_key": "simple_value"}) == {"simpleKey": "simple_value"}18def test_camelize_simple_mapping_camelize_values() -> None:19 assert camelize({"simple_key": "simple_value"}, camelize_mapping_values=True) == {20 "simpleKey": "simpleValue"21 }22def test_camelize_nested_mapping() -> None:23 assert camelize(24 {"simple_key": "simple_value", "complex_key": {"nested_key": "nested_value"}}25 ) == {"simpleKey": "simple_value", "complexKey": {"nestedKey": "nested_value"}}26def test_camelize_mapping_nested_iterable() -> None:27 assert camelize(28 {"simple_key": ["first_value", "second_value"]}, camelize_mapping_values=True29 ) == {"simpleKey": ["firstValue", "secondValue"]}30def test_camelize_mapping_not_dict() -> None:31 d = defaultdict(list)32 d["simple_string"].append("simple_value")33 assert camelize(d, camelize_mapping_values=True) == {"simpleString": ["simpleValue"]}34def test_camelize_simple_list() -> None:35 assert camelize(["simple_string"]) == ["simpleString"]36def test_camelize_iterable_string_iterable() -> None:37 assert camelize(["simple_string", ["another_simple_string"]]) == [38 "simpleString",39 ["anotherSimpleString"],40 ]41def test_camelize_simple_set() -> None:42 assert camelize({"simple_string"}) == {"simpleString"}43def test_camelize_simple_tuple() -> None:44 assert camelize(("simple_string",)) == ("simpleString",)45def test_camelize_iterable_with_non_camelized_type() -> None:46 assert camelize(["simple_string", 5]) == ["simpleString", 5]47def test_camelize_iterable_with_mapping() -> None:48 assert camelize(["simple_string", {"simple_key": "simple_value"}]) == [49 "simpleString",50 {"simpleKey": "simple_value"},51 ]52def test_camelize_unknown_type() -> None:53 assert camelize(date.today()) == date.today()54def test_camelize_complex_dictionary() -> None:55 assert camelize(56 {57 "simple_key": "simple_value",58 "list_key": ["list_value"],59 "set_key": {"set_value"},60 5: "hello",61 "nested_key": {"nested_key_again": "nested_value"},62 },63 camelize_mapping_values=True,64 ) == {65 "simpleKey": "simpleValue",66 "listKey": ["listValue"],67 "setKey": {"setValue"},68 5: "hello",69 "nestedKey": {"nestedKeyAgain": "nestedValue"},...

Full Screen

Full Screen

camelize.spec.js

Source:camelize.spec.js Github

copy

Full Screen

...7 it('has a filterCamelize filter', function () {8 expect(camelize).not.toBeNull();9 });10 it('should return camelized strings', function () {11 expect(camelize('test')).toBe('test');12 expect(camelize('test_string')).toBe('testString');13 expect(camelize('test-string')).toBe('testString'); 14 expect(camelize('test_long_string')).toBe('testLongString'); 15 expect(camelize('test-long-string')).toBe('testLongString'); 16 });17 18 it('should return totally camelized strings with a first=true parameter', function () {19 expect(camelize('test', true)).toBe('Test');20 expect(camelize('test_string', true)).toBe('TestString');21 expect(camelize('test-string', true)).toBe('TestString'); 22 expect(camelize('test_long_string', true)).toBe('TestLongString'); 23 expect(camelize('test-long-string', true)).toBe('TestLongString'); 24 });25 26 it('should not camelize trailing underscores', function () {27 expect(camelize('_test_string')).toBe('_testString');28 expect(camelize('_test_long_string')).toBe('_testLongString');29 expect(camelize('test_string_')).toBe('testString_');30 expect(camelize('test_long_string_')).toBe('testLongString_');31 });32 33 it('should deal with (multiple) spaces', function () {34 expect(camelize('the camelize string method')).toBe('theCamelizeStringMethod');35 expect(camelize(' the-camelize string method')).toBe('theCamelizeStringMethod');36 expect(camelize('the camelize string_method')).toBe('theCamelizeStringMethod');37 });38 39 it('should return empty strings for empty/weird inputs', function () {40 expect(camelize('')).toBe('');41 expect(camelize(null)).toBe('');42 expect(camelize(undefined)).toBe('');43 expect(camelize()).toBe('');44 expect(camelize(NaN)).toBe('');45 expect(camelize(beforeEach)).toBe('');46 });47 48 it('should stringify non-string values', function () {49 expect(camelize(0)).toBe('0');50 expect(camelize(1)).toBe('1');51 expect(camelize(-1)).toBe('-1');52 expect(camelize(Infinity)).toBe('Infinity'); 53 expect(camelize(-Infinity)).toBe('-Infinity'); 54 expect(camelize(true)).toBe('true'); 55 expect(camelize(false)).toBe('false'); 56 });...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run molecule automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful