Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

case转换 #83

Open
Huauauaa opened this issue Sep 13, 2024 · 2 comments
Open

case转换 #83

Huauauaa opened this issue Sep 13, 2024 · 2 comments

Comments

@Huauauaa
Copy link
Owner

Sent from PPHub

@Huauauaa
Copy link
Owner Author

Huauauaa commented Sep 13, 2024

function camel (data) {
  if (typeof data != 'object' || !data) return data  
  if (Array.isArray(data)) {
    return data.map(item => camel(item))
  }
  
  const newData = {}
  for (let key in data) {
    let newKey = key.replace(/_([a-z])/g, (p, m) => m.toUpperCase())
    newData[newKey] = camel(data[key])
  }
  return newData
}

@Huauauaa
Copy link
Owner Author

// case.js
import _ from 'lodash';

function camelCase(str) {
  return _.camelCase(str);
  // return str.replace(/_([a-z])/g, (p, m) => m.toUpperCase());
}

export function toCamel(data) {
  if (typeof data === 'string') {
    return camelCase(data);
  }
  if (typeof data !== 'object' || !data) {
    return data;
  }
  if (Array.isArray(data)) {
    return data.map((item) => toCamel(item));
  }

  return Object.keys(data).reduce((res, key) => {
    let value = data[key];
    if (typeof value === 'object') {
      value = toCamel(value);
    }
    res[camelCase(key)] = value;
    return res;
  }, {});
}
// case.test.js
import { toCamel } from '../case';

test('toCamel', () => {
  expect(toCamel(null)).toBe(null);
  expect(toCamel(undefined)).toBe(undefined);
  expect(toCamel(1)).toBe(1);
  expect(toCamel(0)).toBe(0);
  expect(toCamel('first_name')).toBe('firstName');
  expect(toCamel(true)).toBe(true);
  expect(toCamel(true)).toBe(true);
  expect(toCamel([])).toStrictEqual([]);
  expect(toCamel({})).toStrictEqual({});
  expect(toCamel({ firstName: null })).toStrictEqual({
    firstName: null,
  });
  expect(toCamel({ firstName: undefined })).toStrictEqual({
    firstName: undefined,
  });
  expect(toCamel({ firstName: 'Harvey' })).toStrictEqual({
    firstName: 'Harvey',
  });
  expect(toCamel({ first_name: 'Harvey' })).toStrictEqual({
    firstName: 'Harvey',
  });
  expect(toCamel({ First_Name: 'Harvey' })).toStrictEqual({
    firstName: 'Harvey',
  });
  expect(toCamel([{ first_name: 'Harvey' }])).toStrictEqual([
    {
      firstName: 'Harvey',
    },
  ]);
  expect(
    toCamel({ first_name: 'Harvey', favorite_thing: ['reading'] }),
  ).toStrictEqual({
    firstName: 'Harvey',
    favoriteThing: ['reading'],
  });
});

Huauauaa added a commit to Huauauaa/leetcode that referenced this issue Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant